Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven 2 <resources> inheritance (parent -> child project)

(also posted on maven-users)

Wondering if anyone can shed some light on inheritance of the elements in pom.xml as relates to resource processing and the WAR plugin.

The documentation for the pom [1] has resources listed under "Elements in the POM that are merged". Some experimentation on my local poms against maven 2.2.1 don't appear to exhibit that behavior. What I see is it looks like is inherited by child projects (in a multi-module build), but that if any of those projects have their own block it replaces the parent, not merged. Is that correct?

Example:

parent-pom.xml
|
|-> child-pom.xml

The following works as I'd expect, with files in dev not included in a final WAR.

parent-pom.xml

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

child-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>

The following change to the child (removing any declaration for src/main/resources) appears to result in src/main/resource not being considered during process-resources, not inheriting from the parent as I'd expected.

child-pom.xml

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

[1] http://maven.apache.org/guides/introduction/introduction-to-the-pom.html s

like image 629
jayshao Avatar asked Jun 09 '10 17:06

jayshao


People also ask

Can a Maven project have 2 parents?

Maven does not support multiple inheritance and a project can have only one parent. The parent project can have its own hierarchy.

What is parent project in Maven?

A parent pom. xml file (or super POM) in Maven is used to structure the project in order to avoid redundancies and duplicate configurations by using an inheritance between different pom. xml files. If any dependency or properties are configured in both - parent and child - pom.

How do I merge two Maven projects?

let's assume that you have two maven projects A and B. Now create a new project C in your intllij-idea. In project C, create a new module A1. Copy the dependencies and build, properties (if present) from pom.


2 Answers

Indeed, that's what the documentations says. But I confirm that Maven inheritance overrides resources instead of adding to them. This is actually captured in MNG-2751, and indirectly in MNG-2027, that you might want to bump.

TBH, I'm very curious to see what the maven folks will say on this (I'm personally happy with the current behavior, I don't really want child poms to be "polluted" by specific needs, like exclusions, in a hierarchy) and changing this behavior might break a lot of projects.

like image 73
Pascal Thivent Avatar answered Sep 21 '22 21:09

Pascal Thivent


As noted in adding additional resources to a maven pom, this can be worked around using the build-helper plugin.

like image 21
Grégory Joseph Avatar answered Sep 22 '22 21:09

Grégory Joseph