Implement a Maven Archetype

Maven Archetype is a feature of Maven, you can understand it roughly, it allows us to quickly create a Maven project with a predefined structure. We just need to change values ​​like Group Id, Artifact Id, and Version according to the value we want. I usually use Maven Archetypes to define the structure of Spring or Jakarta EE projects. In this tutorial, I will show you how to implement a Maven archetype so that if you intend to implement a Maven Archetype for yourself, you can rely on my instructions to do it!

The first thing, I need to tell you is that Maven Archetype is also a Maven project but its packaging is not jar, war, or pom but has the value “maven-archetype”. You can use the command line or a Java IDE that supports the Maven project to generate a Maven Archetype project.

Create a new Maven Archetype project

In this tutorial, I will use Eclipse IDE to work with the Maven Archetype project.

I will create a new Maven project using a Maven Archetype of Maven to generate a Maven Archetype project 🙂

This Maven Archetype is named maven-archetype-archetype:

Declare project information:


The files and folders located in the src/main/resources/archetype-resources folder will be in the project that we generate from the Maven Archetype project. For example, if you run “mvn clean install” for this project now (you should use the command line to run it, running in Eclipse may have an issue related to “${maven.home} is not specified” as a directory”) of the Maven plugin, then go to the target/test-classes/projects/id-basic/project directory, you will see a new project generated from our Maven Archetype project:

Import this Maven project using another IDE like IntelliJ, you will see the following results:

Now, we will try to implement the Maven Archetype project to our liking!

Implement Maven Archetype project

As an example for this tutorial, I will implement a Spring MVC project based on the project template of Spring Tool Suite 3, in the tutorial about  Create Spring web application using Spring Legacy Project in Spring Tool Suite 3. For your understanding, Spring MVC project template of Spring Tool Suite 3 is using old versions of the Spring framework and other libraries, every time I create a new Spring MVC project using it, I have to edit it to suit the current time.

OK, let’s get started guys! You can re-read the tutorial Create Spring web application using Spring Legacy Project in Spring Tool Suite 3 to understand the directory files that you need to create in the Maven Archetype project example in this tutorial. The example project of the above tutorial, I also pushed to GitHub at

First, we update the pom.xml file in the src/main/resources/archetype-resources directory of the Maven Archetype project.

I will replace it with the contents of the pom.xml file in the spring-mvc-example project above, then edit the versions of the libraries and frameworks to use the latest version.

The content of the pom.xml file of the Maven Archetype project is now as follows:

The problem we need to fix so that users can generate groupId, artifactId, version according to their needs is to change the configuration groupId, artifactId, version in this pom.xml file.

We will define 3 properties as ${groupId}, ${artifactId}, ${version} and then replace groupId, artifactId, version in pom.xml file with these properties, as follows:

The value of these properties will be entered by the user when generating their project from our Maven Archetype project.

Next, we will copy the HomeController class, package com.huongdanjava.springmvc in the spring-mvc-example project, and put it in the src/main/resources/archetype-resources/src/main/java directory of the Maven Archetype project.

When generating a project using Maven Archetype, users can set the package name according to the name they want. The question is how our Maven Archetype project can generate the right package that the user wants. The solution here is that we will declare using properties ${package} to do this.

For example, with the HomeController class, after copying, I will edit the code that declares the package as follows:

If your Maven Archetype project has other sub-packages, for example, in your spring-mvc-example project, there is an additional package called com.huongdanjava.mvc.test, then the class, in the package com.huongdanjava.mvc .test, needs to declare the package code line as follows:

package ${package}.test;

In my example Maven Archetype project, the App class is no longer needed, so I will remove it.

For the log4j.xml file in the src/main/resources directory, I will create a new resources directory in the src/main/resources/archetype-resources/src/main directory of the Maven Archetype project, to copy this file over. 

To copy the folder files in the webapp folder of the spring-mvc-example project, I will also create a new webapp folder in the src/main/resources/archetype-resources/src/main folder of the Maven Archetype project.

In the src/main/resources/archetype-resources/src/main/webapp/spring/appServlet directory, there is a servlet-context.xml file, which declares a configuration of the Spring framework component-scan, the base-package will be associated to the project that the user will generate, so I will modify it using the ${package} property as follows:

The AppTest class in the src/main/resources/archetype-resources/src/test/java directory is no longer needed, you can remove it.

After having the necessary resources for the Maven project to be generated, you need to update the archetype-metadata.xml file located in the src/main/resources/META-INF/maven directory.

This file is used to define the files and folders that will be generated into the project that the user wants. By default, the contents of this file are as follows:

Already have the src/main/java and src/test/java directories predefined, we need to configure the src/main/webapp directory further:

You notice that when declaring for the webapp directory, I did not declare the “package=true” attribute in the <fileSet> tag, so that Maven Archetype does not put this folder in a package.
At this point, we have completed the implementation of a Maven Archetype project, guys!

You can run “mvn clean install” again and refresh the imported project in the IDE like me above, you will see the same result as me:

Run “mvn clean jetty:run” for this project, you will see the same results as I did in the tutorial Create Spring web application using Spring Legacy Project in Spring Tool Suite 3

One more thing, you might be wondering, where do I get the groupId, artifactId, and version of the generated project in my example? It’s in the file in the src/test/resources/projects/it-basic directory guys! The contents of this file are as follows:

This file is used by maven-archetype-plugin to generate the example project for us.

Add Comment