Multi-module Maven Project
Do you ever sometime find a project that needs to be broken into modules? Yeah, that’s the S
in SOLID, i.e., Single Responsibility Principle.
Fear not, there’s Maven for that. Keep in mind, there are also other build tools than Maven that provides multi-module functionality. For this, I’ll
choose to use Maven because it’s easy for a small project. For a larger project, I’d recommend using Gradle.
To get started, you could follow the steps to create multi-module project.
- Let’s use Maven archetype and navigate into that folder:
mvn archetype:generate -DgroupId=com.jmsweb -DartifactId=multi-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false; cd multi-module
. - Open
multi-module/pom.xml
and edit the file to add<packaging>pom</packaging>
after<url>...</url>
. This will become the parent POM for modules to build under. - Once inside multi-module directory, run the archetype command one more time to generate a JAR module:
mvn archetype:generate -DgroupId=com.jmsweb -DartifactId=child-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false; cd child-module
. - Open
child-module/pom.xml
and edit the file to add<packaging>jar</packaging>
after<url>...</url>
. This will become java JAR module to to build. - The parent and child modules need to know about each other. Let’s open the
child-module/pom.xml
and add the following snippet:
<parent> <groupId>com.jmsweb</groupId> <artifactId>multi-module</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
- The parent module need to know about its child modules. Let’s open the
multi-module/pom.xml
and add the following snippet:
<modules> <module>child-module</module> </modules>
- If done correctly, you should be able to execute the command:
mvn clean install
in the multi-module directory. Maven will build the project and assemle the code into a nice JAR file underchild-module/target/child-module.jar
.
I’ve done this so many times that I can literally do this in my sleep, from command line, or from IDE. My apologizes if I did not provide enough steps. You can always get more information from Apache Maven Project website.