Tuesday 9 July 2019

Maven: Project Inheritance


Sometimes many projects share the common configuration properties, project dependencies. It is always better to keep the common configurations, dependencies in parent pom and let the child poms inherit the parent configurations.

How to specify parent project details?
By using <parent> element, you can specify the parent project details.
         <parent>
                  <groupId>org.parentProject</groupId>
                  <artifactId>org.parentProject</artifactId>
                  <version>1</version>
         </parent>

Sample pom looks like below.
<project>
 <modelVersion>4.0.0</modelVersion>
 
 <parent>
  <groupId>org.parentProject</groupId>
  <artifactId>org.parentProject</artifactId>
  <version>1</version>
 </parent>

 <artifactId>child project1</artifactId>
 <groupId>child project1</groupId>
 <version>1</version>
 
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
 </dependencies>
</project>

How can I see the final pom, after merging with parent pom?
Run the command "mvn help:effective-pom", it prints the final pom file after merging.

How can maven knows the parent POM?
Maven checks for the parent pom in local repository, parent directory of the current project.

You can even specify the parent pom file location by using <relativePath> element.

         <parent>
                  <groupId>org.parentProject</groupId>
                  <artifactId>org.parentProject</artifactId>
                  <version>1</version>
                  <relativePath>../../../pom.xml</relativePath>

         </parent>

Previous                                                    Next                                                    Home

No comments:

Post a Comment