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.

Reference: Maven Archetype Creation Tips from our JCG partner David Carver at the Intellectual Cramps blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


nine − = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close