Maven POM Hierarchy

 

Overview

* POM: project object model
* POM is an xml file
* Used to build a project

Project Inheritance

* Following elements in POM are merged:
– dependencies
– developers and contributors
– plugin lists (including reports)
– plugin executions with matching ids
– plugin configuration
– resources

Super POM

* All POMs extends the super POM
* Default package: jar
* Default repository is central repository: https://repo1.maven.org/maven2/

Minimal POM

* Mandatory elements:

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

* Other elements inherit from super POM

Parent POM

* Specify parent pom:

<project>
  <modelVersion>4.0.0</modelVersion>
  <!-- can be omitted if same as parent -->
  <groupId>com.mycompany.app</groupId>
  <version>1</version>
  <!-- end -->
 
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
	<!-- if parent pom not in the dir one level above
    <relativePath>../parent/pom.xml</relativePath>
	-->
  </parent>
 
  <artifactId>my-module</artifactId>
</project>

Project Aggregation

* Set packaging to: pom
* Specify child POMs as modules:

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>my-module</module>
  </modules>
</project>

Parent POM with Aggregation

* You can specify parent pom to also include modules:

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>my-module</module>
  </modules>
</project>

* Child POM:

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <relativePath>pom.xml</relativePath>
  </parent>
 
  <artifactId>my-module</artifactId>
</project>

Project Model Variables

* Variable values defined in child POM take precedence over parent values
* Project model variables
– any single value element can be referenced as a variable, e.g.:

${project.groupId}
${project.version}
${project.build.sourceDirectory}
${project.basedir}
${project.baseUri}
${maven.build.timestamp}

Project Properties

* Any properties defined in project can be referenced as a variable:

  <properties>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
  </properties>

References

* Introduction to the POM
* POM Reference

This entry was posted in maven and tagged . Bookmark the permalink.