How do I add a project as a dependency of another project?
Author: Deron Eriksson
Description: This tutorial describes how to add a project as a dependency of another project.
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)

Now, I'll add a new dependency in the 'mytest2' pom.xml file for the 'mytest' dependency.

New dependency for 'mytest' in 'mytest2' pom.xml

		<dependency>
			<groupId>com.maventest</groupId>
			<artifactId>mytest</artifactId>
			<version>1.0-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>

The updated mytest2 pom.xml file is shown here.

mytest2 pom.xml

Next, I'll perform a "mvn eclipse:eclipse" on the mytest2 project to update its classpathW.

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

The console output from 'mvn eclipse:eclipse' on 'mytest2' is shown here:

Console output from 'mvn eclipse:eclipse' on 'mytest2'

[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\mvn-eclipse-cache.properties
[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] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Feb 11 17:24:52 PST 2008
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------

The mytest2 .classpath after the eclipse:eclipse goal is shown here. Notice that it contains a reference to the mytest jarW file (mytest-1.0-SNAPSHOT.jar).

mytest2 .classpath after 'mvn eclipse:eclipse'

<classpath>
  <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/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>

We can now add the MyTest import to the MyTest2.java file since the mytest jar file is in the project classpath. I do this via the EclipseSW 'Organize Imports' option.

Organize Imports

The import was successfully added, and the error goes away.

Error is gone. Yippee!!!

Now, if I execute the MyTest2 class, we can see that it successfully references the MyTest class in the mytest jar file.

Console output of execution of MyTest2 class

The final mytest2 pom.xml file is shown here.

final 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>
Page: < 1 2