Saturday, May 12, 2012

Maven Archetypes Part 2: How do I create my first jar?

introduction and review

Before you start with this article, you need to have a basic understanding of Maven archetypes or have read the first article in my guide.  If you haven't, I have a feeling most of this won't make sense to you.

That said, let's start off with a quick review.  In the last article, we left things with an archetype layout of:

This is the most basic archetype you can really have; all it does is create a pom artifact. Typically, you won't be interested in generating a pom from an archetype; you'll want to at least create a jar, and potentially a multi-module project. For the next step, let's create our first jar.

Updating the project pom file to create a jar

Before we start to add files for our archetype, let's update src/main/resources/archetype-resources/pom.xml to change the packaging to jar. Let's also add a dependency on junit so that we can package a unit test in the project that's created, as well as the compiler setting:

Creating the necessary directories and updating the archetype metadata

Next, let's add the default directories that Maven projects use. I've added __packageInPathFormat__ as a subdirectory of both the main and test paths. When you generate a project from an archetype, Maven will create the property packageInPathFormat for you, which changes all of the dots in a package name to slashes. For example, if you have a package of com.theotherian, the packageInPathFormat property will be com/theotherian.

We also need to update the src/main/resources/META-INF/maven/archetype-metadata.xml file so that it knows to include and filter our new directories:

creating a basic Java class and a unit test

Now that we have our directories set up, let's create some source code.
First, let's add your standard "Hello world!" Java class in the file src/main/resources/archetype-resources/src/main/java/__packageInPathFormat__/

Next, let's add a basic unit test just to flesh things out a bit more at src/main/resources/archetype-resources/src/test/java/__packageInPathFormat__/

Wrapping things up

Now that we've added all of this additional stuff, let's do a quick recap of the project layout:

If your project looks like that, go ahead and install the archetype and go back a directory.

creating a project from the archetype

Let's generate another project from our local archetype (if you haven't included this archetype in your local catalog, follow the steps in part 1):

Our project from the archetype has been created, so let's try building it:

Awesome, you've got an archetype building a jar!

But wait, there's more! Check out the next post in this series, which shows you how to build multi-module archetypes, to learn more!


  1. I bought this watch for my dad on this website on Father's Day.Replica Audemars Piguet Watches This watch has been bought since my father has been wearing it. Replica Audemars Piguet royal oakIt has been running and working very hard. The key is that it looks no different from genuine and the design is perfect. He likes it very much.

  2. I read that Post and got it fine and informative. OMC online parts

  3. Manual machining needs one operator per machine and one supervisor for the group, which also requires plenty of coaching activities and workpieces to organize the personnel to Boob Dress Tape soundly perform operations. On the other hand, with CNC machining, one trained operator can run quantity of} autonomous CNC machines simultaneously, and one programmer can load the wanted designs. Not only do you significantly scale back coaching prices, but you also reduce down labor prices. You can use the financial savings from the labor prices to enhance different services that may entice extra customers, inserting you in a extra competitive position on the market.