A quickstart Maven archetype for Java 8

As a Maven rookie, I often use the quickstart archetype from Maven when I want to create a new Maven project. Unfortunately, that archetype is a bit outdated, which means I have to tweak some details before I can actually use it. I guess I got a bit tired of this and I thought I could create my own archetype that is ready to use.

In case you aren’t familiar with the term, an archetype is another word for a template. It’s possible to use an archetype to create a new project and get some boilerplate code and configuration for free. The most basic archetype is arguably maven-archetype-quickstart . This once gives you:

  • a minimal pom.xml file
  • a java App that prints Hello World
  • an example unit test file using jUnit

This archetype is a bit dated. When I use it, I know that I have to correct a few problems:

  • set the Java version to 1.8 in the pom. Since it doesn’t specify a Java version, it defaults to 1.5. That’s old. Very old.
  • upgrade the jUnit dependency from 3.x to the latest, which is now 4.12. This is a major version upgrade, which means that the dummy unit test itself needs to be rewritten in the 4.x style (which is quite shorter by the way)
  • fix indentation and format the files
  • finally, a personal pet peeve, because this is who I am: correct the spelling of the word “rigorous”. It’s spelled “rigourous” in the original sample unit test AppTest.java, and it stings like an eyelash trapped in my eyeball every time I see it.

So I thought I could create my own archetype. Turns out it’s not very difficult, far from it. I found a guide in the official maven documentation. Eventually I only needed the very last part of the documentation (all the way at the end) which reads “Alternative way to start creating your archetype”. Turns out, they have an archetype for creating archetypes:

mvn archetype:generate
 -DgroupId=[your project's group id]
 -DartifactId=[your project's artifact id]
 -DarchetypeArtifactId=maven-archetype-archetype

Not only that, but the skeleton it creates is based exactly on the archetype I wanted to fix (maven-archetype-quickstart). So this generates the files that I wanted to fix. Easy-peasy. The folder structure is stored in the folder src/main/resources/archetype-resources , containing the files this archetype will generate.

To be able to use the archetype, I have to install it in the local maven repository by running mvn install. In order to use it, it’s no different than using any other archetype:

mvn archetype:generate -DgroupId=com.mycompany.myapp \
    -DartifactId=myapp \
    -DarchetypeGroupId=ngeor.archetype-quickstart-jdk8 \
    -DarchetypeArtifactId=archetype-quickstart-jdk8 \
    -DarchetypeVersion=1.0.0 \
    -DinteractiveMode=false

This is it. The archetype is available in github so you can also use it if you like it.

Other things a future version (or a different archetype) could include:

  • add mockito dependency
  • add a .gitignore file if it is possible
  • add JaCoCo for code coverage
  • add a .travis.yml file to enable building with Travis CI (useful for open source projects in GitHub)
  • add a README.md which already has the markdown code for the Travis badge

Hope this helps.

 

Advertisements

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