How to Create Maven Multi Module using Command Line

In this tutorial, we will create a simple Maven multi-module project using the command line. We will use a blogger web application as an example, which will consist of a parent project and three sub-modules:

We'll use the latest versions of Java and dependencies throughout the process.

Prerequisites

Step 1: Create the Parent Project - Blogger

First, open your command line terminal and run the following command to create the parent Maven project:

mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command creates a basic Maven project. Note that the whole command should be a single line.

After the build is successful, you will see output similar to the following:

[INFO] ------------------------------------------------------------------------
            [INFO] BUILD SUCCESS
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time: 01:09 min
            [INFO] Finished at: 2023-07-09T13:40:40+05:30
            [INFO] ------------------------------------------------------------------------

Step 2: Update the pom.xml to Declare It as a Parent Project

Open the pom.xml file of the newly created parent Maven project (blogger) and change the packaging to pom. This is necessary to declare it as a parent project.

Here's the complete 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.companyname.blogger</groupId>
                    <artifactId>blogger</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <packaging>pom</packaging>
                
                    <name>blogger</name>
                    <url>http://maven.apache.org</url>
                
                    <properties>
                        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                        <maven.compiler.source>21</maven.compiler.source>
                        <maven.compiler.target>21</maven.compiler.target>
                    </properties>
                
                    <modules>
                        <module>blogger-core</module>
                        <module>blogger-common</module>
                        <module>blogger-web</module>
                    </modules>
                </project>
                

Step 3: Create Sub-Modules

Change the directory to the parent project directory:

cd blogger

Create blogger-core Module

Run the following command to create the blogger-core module:

mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-core -DinteractiveMode=false

Create blogger-common Module

Run the following command to create the blogger-common module:

mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-common -DinteractiveMode=false

Create blogger-web Module

Run the following command to create the blogger-web module. Note that this module will be a web application packaged as a WAR file:

mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Step 4: Update Sub-Modules pom.xml

blogger-core Module pom.xml

Update the pom.xml of the blogger-core module to set the packaging type to jar:

    <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>
        <parent>
            <artifactId>blogger</artifactId>
            <groupId>com.companyname.blogger</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <groupId>com.companyname.blogger</groupId>
        <artifactId>blogger-core</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>blogger-core</name>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>21</maven.compiler.source>
            <maven.compiler.target>21</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.9.3</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

blogger-common Module pom.xml

Update the pom.xml of the blogger-common module to set the packaging type to jar:

        <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>
            <parent>
                <artifactId>blogger</artifactId>
                <groupId>com.companyname.blogger</groupId>
                <version>1.0-SNAPSHOT</version>
            </parent>
        
            <groupId>com.companyname.blogger</groupId>
            <artifactId>blogger-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <packaging>jar</packaging>
        
            <name>blogger-common</name>
        
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>
            </properties>
        
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-api</artifactId>
                    <version>5.9.3</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </project>
        

blogger-web Module pom.xml

Update the pom.xml of the blogger-web module to set the packaging type to war:

    <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>
        <parent>
            <artifactId>blogger</artifactId>
            <groupId>com.companyname.blogger</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <groupId>com.companyname.blogger</groupId>
        <artifactId>blogger-web</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>blogger-web</name>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>21</maven.compiler.source>
            <maven.compiler.target>21</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>jakarta.servlet</groupId>
                <artifactId>jakarta.servlet-api</artifactId>
                <version>6.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.9.3</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>blogger-web</finalName>
        </build>
    </project>
    

Step 5: Build the Multi-Module Project

Navigate back to the parent project directory (blogger) and run the following command to build the entire multi-module project:

mvn clean install

The output will be similar to the following:

[INFO] Reactor Summary:
    [INFO]
    [INFO] blogger ................................ SUCCESS [  0.746 s]
    [INFO
    
    ] blogger-core ........................... SUCCESS [  5.727 s]
    [INFO] blogger-common ......................... SUCCESS [  1.775 s]
    [INFO] blogger-web ............................ SUCCESS [  0.809 s]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 9.289 s
    [INFO] Finished at: 2023-07-09T14:20:05+05:30
    [INFO] ------------------------------------------------------------------------

Step 6: Import the Project into Eclipse

To import the project into Eclipse:

  1. Open Eclipse.
  2. Go to File -> Import.
  3. Select Maven -> Existing Maven Projects.
  4. Browse to the parent project directory (blogger).
  5. Click Finish.

Conclusion

In this tutorial, we learned how to create a Maven multi-module project using the command line, using a blogger project as an example. We created a parent project and three sub-modules, updated the pom.xml files accordingly, and built the entire project. Maven's multi-module capability helps in managing complex projects efficiently by allowing a logical separation of concerns and dependencies.