Sunday, January 2, 2011

Use Maven archetypes to create your source code structure

Maven (currently in version 3) comes with a great set of archetypes that can be used to create many of the most common source code projects. Instead of starting with an empty POM and setting up your own directory structure Maven can create it all for you. The POM is filled with settings for compiling, running and testing your empty project. It even contains dependency declarations for the most common frameworks and tools as specified by the archetype you choose to use.

IDEs generally have good support for Maven projects. In NetBeans, my IDE of choice, Maven projects are first class citizens. No conversion is needed, the project can be opened right away without any IDE-specific files being created that pollute the source directories. In the current stable version of NetBeans (6.9.1) Maven 2 is used. Since I wanted to run on Maven 3 I choose to create my Maven project at the command line and then open it up in NetBeans. NetBeans comes with wizards for creating projects, but when using it I ended up with a strange mix between the Maven 2 support in NetBeans and my installed Maven 3 runtime. Anyway using Maven direct from the command line feels better since I'm planning on building my project on Hudson later on.

In my example I created a project for a simple Scala application which out-of-the-box includes directories for production code and test code, a stub application-class and a POM with test time dependencies for JUnit, ScalaTest and Specs. This gives me a very valuable set-up with very little effort. Only thing I needed  to do was to move into the directory where I wanted the project to be created and type:

mvn archetype:generate

This command starts up an interactive session where I'm allowed to choose among a really large set of archetypes and then enter the unique identifiers for the project needed by Maven (GroupId and ArtifactId). And that's all! Now the project is ready to build and install into the local Maven repository. Of course, it doesn't do much yet... But I really recommend you to have a look at what Maven archetypes has to offer next time you are starting up a new source code project.

No comments:

Post a Comment