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 >

A key concept in mavenSW is the idea of a repository. A repository is essentially a big collection of "artifacts", which are things like jarW, warW, and earW files. Rather than storing jars within projects (such as in a "lib" directory) on your machine, jars are stored instead in your local maven repository, and you reference these jars in this common location rather than within your projects. In addition, when you build a project into an artifact (typically a jar file), you usually "install" the artifact into your local maven repository. This enables other projects to use it.

Let's see how we can one project as a dependency to another project. Suppose we have two very simple projects, "mytest" and "mytest2":

Eclipse Navigator View

The "mytest" project has a MyTest class that outputs a message to standard output.

MyTest.java

package com.maventest;

public class MyTest {

	public static void main(String[] args) {
		sayHello();
	}

	public static void sayHello() {
		System.out.println("MyTest says hello!");
	}
}

The very similar "mytest2" project has a MyTest2 class that outputs a message to standard output.

MyTest2.java

package com.maventest2;

public class MyTest2 {

	public static void main(String[] args) {
		sayHello();
	}

	public static void sayHello() {
		System.out.println("MyTest2 says hello!");
	}
}

The simple pom.xml file out "mytest" is shown here.

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>
	</dependencies>
</project>

The simple pom.xml file out "mytest2" is shown here.

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>
	</dependencies>
</project>

Now, let's suppose that we'd like the MyTest2 class to call MyTest.sayHello(). At this point, "mytest2" can't see anything in "mytest" because there is no classpathW reference in mytest2 to mytest.

MyTest class not visible yet to MyTest2

So, let's perform a "mvn clean install" to install the mytest artifact (ie, the mytest jar file) into the local maven repository.

Executing 'mvn clean install' on 'mytest' project

The console output from the "mvn clean install" on the mytest project is shown here:

Console output from 'mvn clean install' on 'mytest'

[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.037 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 17:22:19 PST 2008
[INFO] Final Memory: 12M/22M
[INFO] ------------------------------------------------------------------------

(Continued on page 2)

Page:    1 2 >