You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven构建自动添加资源文件夹问题及额外资源目录配置咨询

问题解答

为什么src/main/resources会被自动识别为资源文件夹?

这其实是Maven核心的**约定优于配置(Convention Over Configuration)**原则在起作用!Maven本身内置了一套标准化的项目结构规则,src/main/resources就是官方定义的默认主资源文件目录——哪怕你不在pom.xml里做任何显式配置,Maven的maven-resources-plugin也会自动把这个目录下的文件当作资源处理,在构建过程中复制到输出目录(默认是target/classes)。

你提到的build-helper-maven-plugin只是在默认规则之外额外添加了src/main/app作为资源目录,完全不会影响Maven的原生默认配置,所以src/main/resources依然会被自动识别。

如何添加src/main/cxf作为额外的资源文件夹?

结合你现有的pom.xml配置,推荐两种实现方式,其中第一种和你当前的插件使用习惯更统一:

方式1:通过build-helper-maven-plugin新增(推荐)

直接在你已有的build-helper-maven-plugin的<resources>节点里,添加一个新的<resource>条目即可:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>add-resource</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>add-resource</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <resources>
            <resource>
                <directory>src/main/app/</directory>
            </resource>
            <!-- 新增src/main/cxf作为资源目录 -->
            <resource>
                <directory>src/main/cxf/</directory>
            </resource>
        </resources>
    </configuration>
</plugin>

方式2:直接配置maven-resources-plugin

如果你不想依赖build helper插件,也可以直接在<build>节点下添加全局资源配置,注意要保留默认的src/main/resources,避免丢失原生的资源处理规则:

<build>
    <!-- 其他已有配置... -->
    <resources>
        <!-- 保留Maven默认的资源目录 -->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <!-- 保留你已有的自定义目录 -->
        <resource>
            <directory>src/main/app</directory>
        </resource>
        <!-- 添加新的src/main/cxf目录 -->
        <resource>
            <directory>src/main/cxf</directory>
        </resource>
    </resources>
    <!-- 其他插件配置... -->
</build>

如果需要对这些资源文件做属性过滤(比如替换${xxx}格式的变量),可以在对应的<resource>节点里添加<filtering>true</filtering>,还能通过<includes>/<excludes>指定需要处理的文件范围。


内容的提问来源于stack exchange,提问作者Vijay Manohar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:52:00