Maven generate archetype

Recently, a colleague and myself decided to brushing some dust off some maven archetypes we created for our employer to fasilitate kick-start development in projects of similar type. Things went smoothly until we hit a problem which wasn´t easily solvable. Searching for a possible solution to this problem on the web, we then discovered that using mvn archetype:create-from-project plugin would give us a great starting point for creating a better archetype.

The steps we went through were as follows:

Generate the arhetype from a project

From the root folder of the working project run the command

mvn archetype:create-from-project

This will create the archetype based on the project it was generated from. Next step, install the archetype…

Tailor the archetype to meet your needs

We wanted to create an archetype that we could use again and again so we needed to tailor the archetype a bit to make it re-usable. This was of course a highly iterative process, we made some changes, installed the archetype (see next step) and generated a test project.

Install the archetype

Move to the generated archetype and run:

mvn install

This will install the archetype so that it can be used to generate new projects. Now you can use the archetype:

Use the archetype

Navigate to the folder where you want to create your new project and type the command

mvn archetype:generate -DarchetypeCatalog=local

You will be prompted for information about the project you are creating, amongst other you need to specify the groupId and artifactId.

After a couple of dozens attempts we finally had something that we were satisfied with. Now our archetype sets up a spring server component with one spring web service set up and exposed, a test client (spring ws that too) and a fitnesse server which tests the web service. The component runs in a jetty container and can be started from both eclipse and from the command line.

References

Maven archetype:create-from-project documentation

Leave a Reply