Maven Multi module tutorial

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

In this tutorial we will show how to use Maven multi-module projects using as sample a remote EJB client application.

The mechanism in Maven that handles multi-module projects is referred to as the reactor. This part of the Maven core does the following:

  •  Collects all the available modules to build
  •  Sorts the projects into the correct build order
  •  Builds the selected projects in order

A multi-module project is defined by a parent POM referencing one or more submodules. In root directory, you will find the parent POM (also called the top-level POM). In the subfolder you will find all the submodules used by the project.

maven multimodule maven tutorial jboss

Here's a simple top-level 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>ejb-parent</artifactId>
    <version>7.1.2-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>JBoss AS Quickstarts: Parent for remote EJB and Java client</name>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <distribution>repo</distribution>
            <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
        </license>
    </licenses>
    
    <properties>

        <version.org.jboss.as.plugins.maven.plugin>7.3.Final</version.org.jboss.as.plugins.maven.plugin>
    </properties>


    <modules>
        <module>server</module>
        <module>client</module>
    </modules>

   <build>
      <plugins>

            <plugin>
               <groupId>org.jboss.as.plugins</groupId>
               <artifactId>jboss-as-maven-plugin</artifactId>
               <version>${version.org.jboss.as.plugins.maven.plugin}</version>
               <inherited>true</inherited>
               <configuration>
                  <skip>true</skip>
               </configuration>
            </plugin>
         </plugins>
   </build>

</project>

Notice that the parent defines a set of Maven coordinates: the groupId is com.sample, the artifactId is simple-parent . The parent project doesn’t create a JAR or a WAR; instead, it is simply a POM that refers to other Maven projects. The appropriate packaging for a project like simple-parent that simply provides a Project Object Model is pom.

The next section in the pom.xml lists the project’s submodules:


<modules>
        <module>server</module>
        <module>client</module>
</modules>

These modules are defined in the modules element, and each module element corresponds to a subdirectory beneath your top level pom.xml. Maven knows to look in these directories for pom.xml files, and it will add submodules to the list of Maven projects included in a build.

Lastly, we define some settings which will be inherited by all submodules. The setting we want to export is the org.jboss.as.plugins which will be used both by the server and the client application. You can add here as well common dependencies and POM inheritance will export these dependencies to all submodules.