How to Create a Maven Archetype

3 min read

There are lots of blogposts out there about this topic. Unfortunately most of them are outdated. Even the Apache Maven Guide itself. Therefore I decided to write this post and update the Apache Maven documentation afterwards.

Why do we need a Maven archetype?

During development we create lots of projects and most of the time there is something like a configuration standard and you have the same files, package structure and so on in all of your projects. That's were Maven archetypes come in. A custom Maven archetype helps other developers to create new Maven projects faster and without copy pasting stuff from an existing project. On the one side it's faster than copy pasting stuff around and on the other side you won't make failures while copy pasting, because there is no chance to forget changing the copied stuff to your current project.

Create a new Maven project

If you want to create a custom Maven archetype you have to create a new Maven project first. The easiest way in my opinion is using the following archetype (if you know an easier way please let me know):
mvn archetype:generate -DgroupId=com.schulz.bennet -DartifactId=sample-maven-archetype -DarchetypeArtifactId=maven-archetype-archetype
Unfortunately it's in an older version, so we have to do change some files to create an up to date archetype. After generating the sample archetype Maven project we have to import in our IDE of choice.


After doing this your project structure looks the image above.

Create the prototype files

Now we can add the files every new Maven project should contain when using this archetype. We simply have to put these files under the archetype-resources directory.

Customize the archetype descriptor

In the next step we have to rename the archetype.xml to archetype-metadata.xml and change the file content to: This file should contain all the files we want to have in our new created projects when using this archetype. So let's add the Jenkinsfile and the src/main/java directory to it. Now every created Maven project will contain a Jenkinsfile, a src/main/resources directory and a sample Java class.

Create the prototype pom.xml

In the last step we have to put everything we want to have in the new projects pom.xml in the prototype pom. The groupId, artifactId and version are placeholders and will be replaced when using the archetype. In addition to these placeholders we can put dependencies, plugins and so on in this pom and it would also be part of the the new created projects.

Install the archetype

Now you have to install the archetype in your local maven repository by change the directory to your archetype and type mvn install on your command line. After that the archetype is available on your machine and you can use it to create preconfigured Maven projects based on your needs of your organization.

Create a new project using the archetype

Finally we can use our created archetype on our command line:
mvn archetype:generate -DarchetypeGroupId=com.schulz.bennet -DarchetypeArtifactId=sample-maven-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -DgroupId=com.schulz.bennet -DartifactId=my-new-project

Have fun with it! :-)

Bye,
Bennet


Be Social, Share!