Question

I want to inherit the dependencies of a (parent) pom.xml in a child project in Maven 2.2.1; i.e. use project inheritance. It seems it is necessary to change the default packaging type from jar to pom in this case.

However, doesn't the Maven2 documentation state that the packaging type pom is necessary for project aggregation, i.e. multimodule projects which use submodules, but not for project inheritance?

<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>example</groupId>
 <artifactId>example-parent</artifactId>
 <version>1</version>

 <dependencies>
   <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.14</version>
   </dependency>
 </dependencies>
</project>

<project>     
 <parent>
   <groupId>example</groupId>
   <artifactId>example-parent</artifactId>
   <version>1</version>
 </parent>

 <modelVersion>4.0.0</modelVersion>
 <groupId>example</groupId> 
 <artifactId>example-child</artifactId>
</project>

But if you call Maven (e.g. mvn clean) with the above configuration, you get an error:

Project ID: example:example-child

Reason: Parent: example:example-parent:jar:1 
 of project: example:example-child has wrong packaging: jar.
Must be 'pom'. for project example:example-child

On other other hand, with the following entry:

<project> 
 ... 
 <packaging>pom</packaging>
 ... 
</project>

in the parent pom.xml, Maven can be executed without any error.

Is this behavior of Maven correct?

Was it helpful?

Solution

As documented in the Inheritance section of the POM Reference:

The packaging type required to be pom for parent and aggregation (multi-module) projects.

So Maven's behavior seems correct to me (and the error message is nicely self explaining).

OTHER TIPS

If you're just wanting to inherit dependencies then I don't think it needs to be type 'pom'. You could have it jar and simply specify it as a dependency of the project which you have as child. However you wouldn't then have the parent/child relationship which is what prevents your parent project being a type other than 'pom'.

To be clear, you inherit the dependencies of all your dependencies ( transitive dependencies ).

As noted by Pascal, the behaviour is correct.

If you're still looking for means to share dependencies between modules, you can consider bundling up the dependencies in question into a pom, and then having your modules both depend on that new "dependencies" pom.

See Maven Book Section 3.6.1 for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top