Maven Archetype Creation Tips

I recently needed to create some Maven Archetypes for the Turmeric SOA project.   A Maven Archetype for those that don’t know is a way to generate a project based off some pre-canned project templates.   In the case of the current Turmeric SOA archetype, it will create a multi-module maven project, that contains the Interface, and Service projects, along with a basic WSDL, and the POMs configured appropriately.   This can be run either at the Command Line  or through the New Maven Project wizard of m2eclipse.   Other IDE’s such as NetBeans, IntelliJ, and others can use it as well, through their support of Maven Archetypes.

The hardest thing with getting started with Maven Archetypes is the relatively poor documentation that exists.  Beyond the most basic use (archetype:create-from-project), which creates an archetype given an existing maven project or parent-project, there isn’t much to go on.   So here are some tips.

Directory/Filename Substitution

There are cases during template generation where you want to output different module names and sometimes different filenames.   To do this rename the portion of the directory or file you want to have substitued with a variable name.   For example,  if you specify __rootArtifactId__ as part of a Directory name or Filename, what ever is entered as the artifactId during the mvn archetype:generate will be substituted.    You can do the same with custom properties.  Example.

Built in properties

There are some built in properties that can be used during substitution.  These are:

  • groupId
  • artifactId
  • rootArtifactId
  • version
  • package – a base java package name, that is placed in src/main/java during project creation.

You can use these in your POM and other file templates as well.

Archetype-metadata.xml

The archetype-metadata.xml stored in the archetype-resources/META-INF/maven folder after the project is created, is where you need to tweak what you want generated and how.

For modules, you need to tweak attributes:

  • id – This is the name of the module that will be generated.
  • dir – the template dir
  • name – the artifact Id that will be put in the pom file.

For the id and name attributes you can use the standard maven properities specification: ${someproperty}.   For the dir, you need to use the special __someproperty__ notation.  In most cases this will be __rootArtifactId__.  An example from the Turmeric SOA archetype.

Required Properties

Required Properties is how you prompt for additional information that can be substituted in your templates.   Any properties you specify will be prompted for during generation.   You can pass them in from the command line as well using the -D option.   Required properties are referenced in your templates as properties and are substituted during generation.

Be careful of specifying default values, default values are not prompted for during generation.  They can still be overridden using the -D option.

Archetype Catalogs

It is important to have an archetype catalog.  If you are using Nexus 1.9.x it will automatically search your repositories and generate an archetype catalog for each repository.  It scans for all archetypes and as soon as an archetype is deployed the catalog entry is updated.    If you don’t have an archetype catalog already, maven can create one for you from your local repository.   Just run mvn archetype:crawl.   The output will be put in your .m2 repository.   Archetypes are important as they allow others to know about your custom archetype, and provide a way for them to be shared and discovered.

Hopefull with these few tips, you can spare yourself some of the headaches that I endured.  My next task is to help the Minerva project have a few Archetypes created, so that eclipse projects can get started with Tycho a bit easier.

Do you have other tips, or items to watch out for?  If so, please feel free to add a comment.


About these ads
This entry was posted in eclipse, maven, turmeric, tycho. Bookmark the permalink.

2 Responses to Maven Archetype Creation Tips

  1. Pingback: Dave Carver: Maven Archetype Creation Tips

  2. Ben Zethmayr says:

    If you strip elements from the archetype-metadata.xml created, before installing the archetype, your custom properties will also be prompted for on archetype:generate.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s