How do I add a transitive dependency to my project's classpath?
Author: Deron Eriksson
Description: This tutorial describes how to use the eclipse:eclipse goal to add a transitive dependency to a project classpath.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


Page:    1 2 >

Transitive dependencies are one of the features that make mavenSW extremely powerful. As an example of a transitive dependency, suppose one project, called "mytest", was dependent on a jarW file, such as the popular commons-lang jar file. Now, suppose a second project, "mytest2", was dependent on "mytest". This means that "mytest2" is also dependent on the commons-lang jar file. Maven can help us handle this situation automatically, eliminating much of the work that is needed to manage jar files across projects. This tutorial will demonstrate how the maven eclipseSW plugin can be used to automatically handle transitive dependencies in relation to a project's .classpath file.

First off, suppose we have two projects, "mytest" and "mytest2".

Eclipse Navigator View

The "mytest" project has a dependency on the commons-lang jar file, as shown here in its pom.xml file.

pom.xml of mytest

<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>mytest</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mytest</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.3</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

Now, I need to update the local maven repository for "mytest" so that the project is built into an artifact (a jar file) and so that the latest pom.xml is placed in the local maven repository. This pom.xml file describes the dependencies of "mytest", so if any other projects utilize the "mytest" artifact, they will know about the dependencies of "mytest". To update the local maven repository for "mytest", I'll perform a "mvn clean install" on "mytest".

Executing 'mvn clean install' on 'mytest' project

The output of 'mvn clean install' on 'mytest' is shown here.

Console output of 'mvn clean install' on 'mytest' project

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory C:\dev\workspace\mytest\target
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 2 source files to C:\dev\workspace\mytest\target\classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to C:\dev\workspace\mytest\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\dev\workspace\mytest\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.maventest.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar]
[INFO] Building jar: C:\dev\workspace\mytest\target\mytest-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing C:\dev\workspace\mytest\target\mytest-1.0-SNAPSHOT.jar to \dev\m2repo\com\maventest\mytest\1.0-SNAPSHOT\mytest-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Mon Feb 11 21:47:18 PST 2008
[INFO] Final Memory: 12M/22M
[INFO] ------------------------------------------------------------------------

The "mytest2" project is dependent on the "mytest" project, as shown here in the pom.xml file for "mytest2".

pom.xml of mytest2

<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>mytest2</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mytest2</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.maventest</groupId>
			<artifactId>mytest</artifactId>
			<version>1.0-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

(Continued on page 2)

Page:    1 2 >