Create Maven Archetype for Maven multi-module project

In the previous tutorial, I showed you how to create a Maven Archetype for a normal Maven project. For Maven multi-module projects, how to create Maven Archetype? We will find out together in this article!

As an example, I will take an example project of Clean Architecture in the article Introduction about Clean Architecture – Part 2 to build a Maven Archetype for this example project. You can find the source code of this example project here https://github.com/khanhnguyenj/huongdanjava.com/tree/master/clean-architecture-example.

First, I will also create a new Maven Archetype project using Maven Archetype maven-archetype-archetype, the results are as follows:

First, I will copy the contents of the pom.xml file in the clean-architecture-example project and replace the contents of the pom.xml file at the project’s src/main/resources/archetype-resources directory.

We will edit the version of the dependencies so that they are updated to the latest version!

The content of pom.xml file of my Maven Archetype project will be as follows:

We will use the placeholders ${groupId}, ${artifactId}, and ${version} to replace the groupId, artifactId, and version information defined above so that when generating the project from our Maven Archetype project, these information is taken from user input:

Next, we will copy the module directories from the clean-architecture-example project to the src/main/resources/archetype-resources directory of the Maven Archetype project.

The following results:

In order for the modules to be generated as we want, like the clean-architecture-example project, we need to edit a little bit of the content of the module directories.

Specifically, for the configuration module, in the pom.xml file of this module, we will use the placeholders ${groupId}, ${rootArtifactId} and ${version} to replace the information about groupId, artifactId and version of the parent and module dependencies. The value of the placeholder ${rootArtifactId} will be the artifactId value that the user input!

The contents of the pom.xml file in the configuration directory will look like this:

For the Application class, we will move it to the /src/main/java directory and replace the package name with the placeholder ${package} as follows:

Since the UseCaseConfiguration class is in a sub-package, we will move this class to the src/main/java/configuration directory and edit its content as follows:

Please notice that I have also replaced the placeholder ${package} in the import lines so that when generating the project, the package information will be taken from the user input.

For the remaining modules, please do the same!

The last step that we need to do is configure the archetype-metadata.xml file located in the src/main/resources/META-INF/maven directory to include all the necessary files and directories when generating the Maven project using our Maven Archetype project.

We will use the <modules> tag along with the <module> tag to include the module directories into the project in the archetype-metadata.xml file, specifically as follows:

As you can see, I have defined each module in our project using a <module> tag. In each <module> tag, we define <fileSets> to copy the directory files in each module as we want, like when we create Maven Archetype project for normal Maven projects.

At this point, we have finished configuring the Maven Archetype for the Maven multi-module project!

Please run “mvn clean install” in the root directory of the project and then import the project basic-project in the directory

target/test-classes/projects/it-basic/project/ into a certain IDE, you will see the following results:

Add Comment