Monday, May 28, 2012

Defining project relationships in maven


Maven provides three ways to define project relationships

Dependency – Dependency relationships are useful when one wishes to use some API in the project/framework. Since the advent of maven 2.x dependency relation ship has got much stronger & usable, where one just needs to declare the project dependency and maven itself take care of transitive dependency.  
To define dependency, one needs to mention the repository, if jar does not exist in maven’s default repository and add dependency in pom.xml. One can define the repository in settings.xml as well but that would reduce the projects portability.

The example of declaration to include javaee in your project is as mentioned below –
 <repositories>
  <repository>
   <id>Java.Net</id>
   <url>http://download.java.net/maven/2/</url>
  </repository>
 </repositories>

 <dependencies>
  <dependency>
   <groupId>javax</groupId>
   <artifactId>javaee-api</artifactId>
   <version>6.0</version>
  </dependency>
 </dependencies>

Inheritance – Inheritance as the name suggests let the child projects inherit the parent’s elements. One can use inheritance in multi module project by making a parent project, extracting the common elements of module’s POM and putting them in parent project’s POM. After declaring the parent relationship in child’s POM the elements automatically become available in child project.

Maven provide the inheritance of following elements –
  • dependencies
  • developers and contributors
  • plugin lists
  • reports lists
  • plugin executions with matching ids
  • plugin configuration
Example of parent declaration in maven 3.0 project is as mentioned below -

    <parent>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-parent</artifactId>
  <version>21</version>
  <relativePath>../pom/maven/pom.xml</relativePath>
  </parent>
Note – In maven all the POMs by default inherit the super POM, which defines the default maven project structure,  default life-cycle and plugin for default life cycle activities

Aggregation – Aggregation is used in muti-module project; where one wants to centralize the build process of all the modules. He/She needs to create a aggregator project and declare all the module’s in it’s POM. During the build phase of aggregator project all the modules are built. The ordering of module does not matter. Maven always builds the dependency first and then project.

Example of aggregation in maven project is as mentioned below –
  <modules>
  <module>maven-plugin-api</module>
  <module>maven-model</module>
  <module>maven-model-builder</module>
  <module>maven-core</module>
  <module>maven-settings</module>
  <module>maven-settings-builder</module>
  <module>maven-artifact</module>
  <module>maven-aether-provider</module>
  <module>maven-repository-metadata</module>
  <module>maven-embedder</module>
  <module>maven-compat</module>
  <module>apache-maven</module>
  </modules>
References -
Super Pom -
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
POM Reference - http://maven.apache.org/pom.html
Maven 3.0 project pom - http://svn.apache.org/viewvc/maven/maven-3/trunk/pom.xml?view=markup

No comments:

Post a Comment