How do I create a multi-module project in Eclipse?
Author: Deron Eriksson
Description: This tutorial describes how to create a multi-module maven project in Eclipse.
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 multi-module project is, as its name suggests, a project that consists of multiple modules, where a module is a project. You can think of a multi-module project as a logical grouping of sub-projects. The packaging of a multi-module project is "pom", since it consists of a pom.xml file but no artifact (jar, warW, earW, etc).

The typical "maven" way to organize multi-module projects is to store them hierarchically, where your modules exist within your multi-module project. Your modules may even have more modules within them. However, the "Eclipse" way to organize projects is in a flat manner, where each module is a project located at the root level of your workspace.

In general, I prefer to do things the "maven" way, but as for project layouts, I prefer to do things the "Eclipse" way and use a flat structure where all projects are located at the same level and not nested within each other. I prefer the "Eclipse" way because it allows me to easily debug across modules when modules are dependent on other modules.

In another tutorial, we created a "mytest" project with a commons-lang dependency, and we created a "mytest2" project that was dependent on the "mytest" project. I did a "mvn clean install" on the "mytest" project so that it was installed into my local mavenSW repository so that the "mytest2" project could use the mytest jarW file.

The above technique works, but it's not really ideal, since the "mytest" and "mytest2" projects really go together. Since the projects are related, we can create a multi-module project to relate them. To do this, I created a multi-module project called "myproject". It is a general (ie, simple) EclipseSW project that contains a pom.xml file. The "myproject", "mytest", and "mytest2" projects are shown here.

Eclipse Navigator View

In my myproject pom.xml file, I set the packaging to "pom". Next, I listed the modules as "../mytest" and "../mytest2". The "../" is used since the modules are Eclipse projects that are located at the same level as the "myproject" project. After this, I removed the junit dependency from the "mytest" pom.xml file and "mytest2" pom.xml file and added it to the "myproject" pom.xml file, since the junit dependency is a common dependency shared by both modules. The "mytest" and "mytest2" projects will now get the junit dependency through inheritance.

myproject 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">
	<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>
	<modules>
		<module>../mytest</module>
		<module>../mytest2</module>
	</modules>
</project>

Next, in the "mytest" pom.xml file, I added a parent section to reference the "myproject" project, which is now the parent project to "mytest". In particular, notice the relativePath element, which is set to "../myproject/pom.xml". This is the relative path to the "myproject" pom.xml file. Notice that the junit dependency is no longer listed in the "mytest" pom.xml file, since we moved it to the parent ("myproject") pom.xml file.

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>
			<version>2.3</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

The changes made to the "mytest2" pom.xml file were identical to the changes made to the "mytest" pom.xml file. Notice however that "mytest2" lists "mytest" as a dependency.

mytest2 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>mytest2</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mytest2</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<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 >