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:

<project xmlns:xsi="" 
 <name>My archetype creator</name>


or use directly the command line:

archetype:generate -DarchetypeGroupId=com.blogger.archetype -DarchetypeArtifactId=myarchetype-creator
-DgroupId=com.blogger -Dpackage=com.blogger.brmoez -DartifactId=brmoez

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:

<archetype-descriptor name="brmoez">
        <!-- required properties -->
  <requiredProperty key="property_1" />
  <requiredProperty key="property_2">
        <!-- fileSets -->
  <fileSet filtered="true" packaged="true" encoding="UTF-8">
  <fileSet filtered="true" encoding="UTF-8">
  <fileSet filtered="true" packaged="true" encoding="UTF-8">

  <fileSet filtered="true" encoding="UTF-8">


  1. Thanks Moez, but what about packaging ?

  2. In fileSet we have an attribute packaged="true", this file will be added to the package later.

  3. 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?