Saturday, September 01, 2007

Specify artifact details only once with the Maven dependencyManagement element

If you have multi-module maven builds, where sub-poms inherit a parent pom, you can utilize the dependencyManagement element in the parent pom, to only define artifact details once. Here is an except from a parent pom:
<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring</artifactid>
<version>2.0.5</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-mock</artifactid>
<version>2.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencymanagement>
The above pom extract does not define dependencies in the project. It sets some defaults, which child-poms inherit. Child-poms will not have the dependencies themselves, unless they explicitly define them in their dependencies element.

In the poms in sub-modules, that inherit this parent pom, you will only need to define the artifacts you depend upon, not their details like version, exclusions, etc. Here is an except from a sub-pom:
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring</artifactid>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-mock</artifactid>
</dependency>
</dependencies>
Using dependencyManagement keeps the version the same over all modules, and reuses other elements like exclusions and scope.

No comments: