Creating Your Own Maven Archetype in 5 Simple Steps

Last Updated:  June 29, 2021 | Published: October 28, 2019

Manually creating a new Maven project is cumbersome. Most companies have a set of shared libraries (e.g. security) and pre-defined configurations they need for each project. Copy and pasting these files over and over is a tedious task. With Maven we have the option to build project templating toolkits with so-called Maven Archetypes. Learn how to create your own Maven Archetype in 5 simple steps with this blog post.

As an example for this blog post, I'll demonstrate how to create a minimal Jakarta EE 8 with Java 11 Maven Archetype.

Archetype project setup

Creating a custom Maven Archetype is simple. You just a Maven project with the following setup:

Compared to normal Maven projects this is a little bit different. First, the packaging type is maven-archetype. While your usual service is packaged as a war or jar, Maven provides a dedicated type to create Archetypes.

Next, you don't need any dependency or plugin but a Maven extension: archetype-packaging. This extension is capable of building the final Archetype later on.

Define your project template

The actual project is located at src/main/resources/archetype-resources. Within this folder, you specify the target project template like a usual Maven project. Here you have to set up the folder structure of your project and include all files you want to use.

For our Jakarta EE 8 project, this might look like the following:

Folder structure for Maven project

You can add any file you like and are not limited to only Java classes. To instruct Maven which files to include, you need to create a archetype-metadata.xml file within src/main/resources/META-INF/maven.

Each fileSet describes a set of files or directories you want to include within your Maven Archetype. The attributes filtered and packaged like you see them in the code snippet above are important to make use of placeholders. For a detailed explanation of the available elements and attributes in this file, have a look at the official documentation.

Add placeholder and dynamic content to your Maven Archetype

To make the resulting project and its files more dynamic, you can add placeholders to your files. Once we use the Archetype we will specify the groupId, artifactId and version of our project. These values can be injected to any file you like:

In this example, I'll use these variables to also prepare the Dockerfile and a bash script:

Please note that this variable replacement only takes place for files or directory you marked as filtered in the archetype-metadata.xml file.

Similarly, if your Archetype contains pre-constructed Java classes, you might want to place them in the target package structure. You can achieve this by setting the packaged attribute in archetype-metadata.xml to true and use ${package} placeholder:

If placeholders and the packaging option are not enough, you can add an archetype-post-generate.groovy script within src/main/resources/META-INF. This file gets executed after Maven successfully creates a new project out of your Archetype. With this file, you can do any customization to the resulting project you want.

As an example, I'll use this file to mark the bash script as an executable file:

Publish your Maven Archetype

Now we are ready to publish the Maven Archetype. For a company internal Archetype, you might choose your internal artifact manager. You can also publish it to the Sonatype Nexus Repository Manager to make it available for everyone.

For a first demo, you can publish the Archetype to your local Maven repository first:

This will install the Archetype in your local repository (~/.m2/repository) and just can start using it.

Make use of your Archetype to bootstrap projects faster

Finally, we can now make use of the Archetype. Maven offers a plugin for this, which we can use with the following command:

The users of your Maven Archetype just need to specify their own groupId and artifactId, that's all.

You can find the code for this Maven Archetype on GitHub.

The Archetypes I use on a regular basis are available here (they are all published to Sonatype).

Have fun creating your own Maven Archetype,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join Our Mailing List To Get 3x Free Cheat Sheets

Free Java Cheat Sheets
>