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 –


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 -

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 –
References -
Super Pom -
POM Reference -
Maven 3.0 project pom -

No comments:

Post a Comment