How do I manage the version of a dependency in a parent POM?
Author: Deron Eriksson
Description: This tutorial describes how to use a dependencyManagement section to manage the version of a dependency in a parent POM.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


A "dependencyMangement" section of a parent POMW can be used to manage the version numbers of dependencies in child projects. If a dependency is listed in the dependencyManagement section of a parent, a child's effective POM is not forced to have this dependency in it. However, if the child does use this dependency, the child POM doesn't need to specify a version number for the dependency since it will get this information from the parent POM's dependencyManagement section.

In a previous tutorial, we created a multi-module project in EclipseSW where "myproject" is a parent project and "mytest" and "mytest2" are child projects. The "mytest" project had a dependency on commons-lang, version 2.3.

Now, I modified the "myproject" pom.xml file (the parent pomW) so that it includes a dependencyManagement section. In it, I specify the 2.3 version of commons-lang.

myproject pom.xml file (parent POM)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.maventest</groupId>
	<artifactId>myproject</artifactId>
	<packaging>pom</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>myproject</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>commons-lang</groupId>
				<artifactId>commons-lang</artifactId>
				<version>2.3</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<modules>
		<module>../mytest</module>
		<module>../mytest2</module>
	</modules>
</project>

I removed the commons-lang version number from the "mytest" pom.xml dependency section, since this version number is now managed by the parent pom.

mytest pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<parent>
		<groupId>com.maventest</groupId>
		<artifactId>myproject</artifactId>
		<version>1.0-SNAPSHOT</version>
		<relativePath>../myproject/pom.xml</relativePath>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.maventest</groupId>
	<artifactId>mytest</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mytest</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

If we perform a "mvn help:effective-pom" on "mytest", we can see that the commons-lang version number that is used in mytest's effective POM is taken from the dependencyManagement section of the "myproject" POM (the parent POM).

mytest effective POM (from 'mvn help:effective-pom')

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest
[INFO]    task-segment: [help:effective-pom] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:effective-pom]
[INFO] 
************************************************************************************
Effective POM for project 'com.maventest:mytest:jar:1.0-SNAPSHOT'
************************************************************************************
<?xml version="1.0"?><project>
  <parent>
    <artifactId>myproject</artifactId>
    <groupId>com.maventest</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../myproject/pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.maventest</groupId>
  <artifactId>mytest</artifactId>
  <name>mytest</name>
  <version>1.0-SNAPSHOT</version>
  <url>http://maven.apache.org</url>
  <build>
    <sourceDirectory>C:\dev\workspace\mytest\src\main\java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\dev\workspace\mytest\src\test\java</testSourceDirectory>
    <outputDirectory>C:\dev\workspace\mytest\target\classes</outputDirectory>
    <testOutputDirectory>C:\dev\workspace\mytest\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\dev\workspace\mytest\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\dev\workspace\mytest\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\dev\workspace\mytest\target</directory>
    <finalName>mytest-1.0-SNAPSHOT</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.0.2</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>target/site</outputDirectory>
  </reporting>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <properties>
    <a.property>this is a property</a.property>
  </properties>
</project>
************************************************************************************


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Feb 12 23:48:07 PST 2008
[INFO] Final Memory: 2M/6M
[INFO] ------------------------------------------------------------------------

A nice thing about using the dependencyManagement feature of mavenSW is that is can centralize dependency version number management all in one location rather than having this information spread out across multiple related projects.