maven resources inheritance problem

如果maven项目中父项目pom.xml里的build > resources项有配置,而子项目pom.xml中的build > resources没有配置时,则子项目直接继承父项目的配置内容。

如果子项目中有配置build > resources,则会覆盖父项目中的全部配置,即父项目中pom.xml中关于build > resources的配置就无效了,不会再被子项目继承,这不同于dependencies > dependencebuild > plugins > plugin,这二个会完整的继承父项目的设置,并且会覆盖相同的配置,合并不同的配置。

所以maven官方JIRA中很多人认为这是一个bug,因为这与继承的直观印象相左,很容易误用,不过这个问题并没有被进一步修正过,状态为CLOSED。

以下就采用stackoverflow.com上用户的提问来简单说明一下:

Example

项目结构如下:

parent-pom.xml

└── child-pom.xml

parent-pom.xml

<resources>    <resource>        <directory>src/main/resources</directory>        <excludes>            <exclude>${dev-config.path}</exclude>        </excludes>    </resource>    <resource>        <directory>src/main/rules</directory>    </resource>    <resource>        <directory>src/test/rules</directory>    </resource><resources>

如果以上配置之后,而子项目中不配置<resources>,那么parent-pom.xml中的设置在子项目完全有效。但是如果在子项目中有如下配置的话,就会完全无视parent-pom.xml中的配置了。

child-pom.xml

<resources>    <resource>        <directory>src/main/resources</directory>        <excludes>            <exclude>${dev-config.path}</exclude>        </excludes>    </resource></resources>

如果在子项目child-pom.xml中有如上配置,虽然只是简单重复了parent-pom.xml中的部分配置,但结果会发现parent-pom.xml中如下所示的2个resource的配置最后发布时没有被打包进生成的jar/war包中。

<resources>    <resource>        <directory>src/main/rules</directory>    </resource>    <resource>        <directory>src/test/rules</directory>    </resource></resources>

这个问题在stackoverflow和maven的官方JIRA里都有很多人提问,可以参考一下,另外官方也没有文档说明resources的继承使用方式。

References

  1. Maven 2 resource inheritance
  2. Resource inheritance isn't additive
  3. parent pom and it's children
  4. Introduction to the POM
  5. POM Reference