Pages

May 14, 2012

How to create a maven archetype

When we try to create a maven project into a java IDE (eclipse, netbeans...), it asks which archetype to use. Of course we can use the default archetype provided by the IDE. In this article I will try to detail how to create our own archetype.

First, we create a simple maven project, with eclipse new -> other -> maven:

After that we modify the pom.xml, automatically generated by eclipse like the following:

Second, we can add some classes or resource files to be generated automatically when creating a project and setting the archetype myarchetype-creator, we can also add some parameters (required properties) to be given to the archetype when creating the project.

The files will be added under src/main/resources.
The parameters of the archetype will be configured in the archetype descriptor, for example:

I am using maven archetypes for generation of code and as given they also use Velocity. Velocity can be used for dynamic file generation, it can use java objects and use there methods, fields etc to generate dynamic file. However as far as maven archetype is concerned I see that they are only capable of simple variable substitution and a static file/folder structure.

Is there a way to do processing prior to creating a project from an archetype, add java objects to the velocity context, and have the archetype templates be able to access them. Is there some documentation about how to use java objects with the maven archetype to generate dynamic file content?