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

(Continued from page 1)

Next, I'll perform a 'mvn eclipse:eclipse' goal on the 'mytest2' project. This will update the .classpath file of "mytest2" so that it is updated to contain a reference to the "mytest" jarW file. However, since the "mytest" pom.xml file also contains a reference to commons-lang, the commons-lang jar file specified in the pom.xml file of "mytest" will be reflected in the .classpath file of "mytest2".

Executing 'mvn eclipse:eclipse' goal on 'mytest2' project

The console output of 'mvn eclipse:eclipse' on the 'mytest2' project is shown here.

Console output of 'mvn eclipse:eclipse' on 'mytest2' project

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest2
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse]
[INFO] Using source status cache: C:\dev\workspace\mytest2\target\
[INFO] Not writing settings - defaults suffice
[INFO] File C:\dev\workspace\mytest2\.project already exists.
       Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed.
[INFO] Wrote Eclipse project for "mytest2" to C:\dev\workspace\mytest2.
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Feb 11 21:41:09 PST 2008
[INFO] Final Memory: 4M/7M
[INFO] ------------------------------------------------------------------------

Let's inspect the .classpath file of the 'mytest2' project. As you can see, it contains a reference to the mytest-1.0-SNAPSHOT.jar file in the local mavenSW repository. In addition, it also has a reference to the commons-lang-2.3.jar file in the local maven repository, since this is a transitive dependency that 'mytest2' knows about from the pom.xml file of 'mytest'. Neato!

.classpath file of 'mytest2' project after 'mvn eclipse:eclipse'

  <classpathentry kind="src" path="src/main/java"/>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="var" path="M2_REPO/com/maventest/mytest/1.0-SNAPSHOT/mytest-1.0-SNAPSHOT.jar"/>
  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.3/commons-lang-2.3.jar"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>

Transitive dependencies are an extremely powerful feature of maven that can help alleviate some of the pain involved in trying to manage jar file dependencies across multiple projects.

Page: < 1 2