How to create multi-module Java project in Maven

In the last post, we saw how to create a simple Java project in Maven. In real world, we need to organize code into modules which can be reused across projects within organisation or even outside as well. In this post, we shall see how we can create a multi-module Java projects using Maven.

Pre-requisite

  • Java 6+
  • Maven 3.2.5

Here are few assumptions

Package name = com.codezuzu (used for groupId)
App name = codezuzu-app (used for artifactId)

We shall create three modules common, core and utils. Let’s see how to create the same

Creating the root pom

$mvn archetype:generate -DgroupId=com.codezuzu -DartifactId=codezuzu-app -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE -DinteractiveMode=false

After the execution of this step, here is how out project looks like

root pom

Creating common module

Let’s the command to create the common module. We would be using non-interactive mode, to use interactive mode remove -DinteractiveMode=false from command, and it shall ask few basic things to customize.

$cd codezuzu-app/
$mvn archetype:generate -DgroupId=com.codezuzu -DartifactId=codezuzu-common -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DinteractiveMode=false

This is very similar to creating simple java module.

After the execution our folder structure looks like this

common module

Create core and util modules

We need to run the same command, with different artifactId’s. Here we go

$mvn archetype:generate -DgroupId=com.codezuzu -DartifactId=codezuzu-core -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DinteractiveMode=false
$mvn archetype:generate -DgroupId=com.codezuzu -DartifactId=codezuzu-util -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DinteractiveMode=false

Now the project is complete. Let’s see how the final directory structure looks like

final modules

As we see, we have all the modules. This process also adds all the modules into the pom.xml in root folder. Here is how the final pom.xml looks like

<?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.codezuzu</groupId>
  <artifactId>codezuzu-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>codezuzu-app</name>
  <modules>
    <module>codezuzu-common</module>
    <module>codezuzu-core</module>
    <module>codezuzu-util</module>
  </modules>
</project>

As we see all the modules are present. You can add more modules in the same way. If you want to drop module from compilation, comment the module here.

Leave a Reply

Your email address will not be published. Required fields are marked *