Create a maven archetype from an existing maven project structure
This recipe details the basic steps one needs to perform in order to create a maven archetype from an existing project to speed up future developments.
The following are the necessary steps for the creation of the archetype (we assume you already have a project with the necessary structure):
- cd $project_home – go to your project root folder
- mvn clean – make sure you start with a fresh build
- mvn archetype:create-from-project – execute the create-from-project goal of the archetype plugin in order to generate the archetype sources in $project_home/target/generated-sources/archetype
- cd target/generated-sources/archetype – go to the archetype generated sources
- mvn clean install – install the project in your local maven repository
To test the archetype you just created, try to create a new project from an existing archetype, by executing mvn archetype:generate, select the archetype from the coordinates you have specified for your original project, fill in the groupId, artifactId, version, package and you should be able to have a new fresh project with the same structure of your original project.
When I implement a new project, it’s very common to start by looking at existing maven archetypes and trying to choose one that fits well my technical needs. However, in all of them I end up doing several changes that I rarely remember to share with myself for similar projects in the future. So, for new projects, I have to repeat the process, once again. In order to avoid that, I have created this recipe as a reminder for myself (and hopefully, others as well!) to create an archetype when you think the project is more or less in a stable version.