Enterprise Java

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.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Justin Holmes
Justin Holmes
9 years ago

Great post David. FYI – the links to the code examples are broken. Looks like they need to be updated to point to github. Thanks for sharing!

Rusi Popov
Rusi Popov
7 years ago

You could define additional properties in the archetype, following the format: https://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html For example: define the file: src\main\resources\META-INF\maven\archetype-metadata.xml ${groupId}.${artifactId} ${groupId} ${artifactId} ${version} Here you see that it defines additional *required* properties, so they have to be mandatorily provided within the dialog, where: * some properties may have no value – see metamodelUrl * some properties may have default values either ** as static text ** or referring the values of the previously defined standard properties: groupId, artifactId, version * some poperties may override the values of the standard properties – the “package” property. Here it is redefined. Please note: *… Read more »

Back to top button