Creating Maven multi-module project with Eclipse

In this post, we will see how to create a maven Multi-module project. Multi-module project structuring can be handy when we have to share a particular module between different projects. For instance, you can have different web-applications which are using same module for interacting with database.

We will develop a parent-project and three sub-modules projects (one model library and two web applications), where both web-applications will share the same model library.

Step 1 : Create Parent project

mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=parent-project

This command will create a new project with name ‘parent-project’ in your workspace.

Step 2 : Update pom.xml to declare it as parent project

Open pom.xml of above created parent-project and change the packaging to ‘pom’.

  <packaging>pom</packaging>

/parent-project/pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.websystique.multimodule</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>parent-project</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Step 3 : Create sub-modules

Via command line, navigate to the directory of parent-project, and fire following commands

cd parent-project
mvn archetype:generate -DgroupId=com.websystique.multimodule  -DartifactId=model-lib
mvn archetype:generate -DgroupId=com.websystique.multimodule  -DartifactId=webapp1
mvn archetype:generate -DgroupId=com.websystique.multimodule  -DartifactId=webapp2

Now if you open the parent-project pom.xml, you will find all three modules being added in there.

  <modules>
    <module>model-lib</module>
    <module>webapp1</module>
    <module>webapp2</module>
  </modules>

Also, in each sub-module’s pom.xml, a parent section being added.

  <parent>
    <groupId>com.websystique.multimodule</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

Step 4 : Update sub-modules pom.xml to produce appropriate output

In our case, model-lib is a libreary(jar), we will set its’s packaging to jar.

  <packaging>jar</packaging>

On the other hand, webapp1 & webapp2 are intended to be WAR, so we need to changed their pom.xml to set the packaging to war.

  <packaging>war</packaging>

Below are the final pom.xml of each sub-module and parent-project

/parent-project/model-lib/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.websystique.multimodule</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.websystique.multimodule</groupId>
  <artifactId>model-lib</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>model-lib</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

/parent-project/webapp1/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.websystique.multimodule</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.websystique.multimodule</groupId>
  <artifactId>webapp1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>webapp1</name>
  <packaging>war</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

/parent-project/webapp2/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.websystique.multimodule</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.websystique.multimodule</groupId>
  <artifactId>webapp2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>webapp2</name>
  <packaging>war</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

And the parent-project pom.xml is

/parent-project/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.websystique.multimodule</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>parent-project</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>model-lib</module>
    <module>webapp1</module>
    <module>webapp2</module>
  </modules>
</project>

Step 5 : Import these projects as ‘Existing maven projects’ in Eclipse

In Eclipse , File->Import->Maven->Existing maven projects

click next, select your workspace as root directory , you should see something similar :

click finish, you should see the projects got imported successfully in Eclipse.

Now when you build the parent-project, all your child project will be build successively.

One remark: Although it’s not mandatory, when you change any of your project, it’s advisable to perform a Maven-> Update project do get the changes reflected in all the projects which depends on this project.

That’s it.

References