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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


9 − three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books