Software Project Management with Maven 2

The one development tool that I use every single day, aside from Eclipse, is Maven. Maven is a software project management tool developed by the Jakarta peeps at the Apache Software Foundation. Maven allows me to build, test, and package our entire Java application in one command. Our application is a proprietary Rapid Application Framework made up of over four thousand Java classes, hundreds of JSP files, dozens of JavaScript files, and tons of XML-based screens (not unlike Mozilla’s XML User Language). Of course, an application our size is broken down into several modules/components and requires a lot of third party jars. Maven grew out of a similar situation at Jakarta.

Maven’s objective says it all, “Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time.” In my development experience, I have seen Java applications built with cryptic PERL scripts and series of Ant build files. Using Maven has been the most pleasant build tool experience to date.

In my opinion, Maven does not requires a big learning curve or large investment in time to get started. You should be able to get started by the time you finish skimming this document.

Maven Setup
In a Windows system, lets imagine you unzip maven to c:\maven. In this situation you will need to add C:\maven\bin to your system %PATH% environment variable. In Windows you can do this via the Advanced tab of the System Properties dialog.

In Mac OS X, if you placed maven in /maven, add /maven/bin to you $PATH variable. In Mac OS X you can add a line to your .bash_login that reads as the following.

export PATH="/maven/bin:$PATH"

In both operating systems, make sure you have defined a JAVA_HOME environment variable that points to your JDK. The JAVA_HOME variable is required by Maven.

Once ‘installed’ you can execute the following command from you command prompt.

mvn --version

As of this writing, the current Maven version is 2.0.5, you might see a different Maven version.

Create New Project
To create a new Maven project execute the following Maven goal.

mvn archetype:create -DgroupId=com.juixe.app -DartifactId=my-app

A brief explanation is in order. The archetype:create is a Maven goal to build a new project. The groupId is a Java system property the defines the base package for the new project. The artifactId defines you Maven project/artifact name. Maven will create a directory for the project named with the value of artifactId property.

The above Maven goal will create a sample Java project with a hello world App.java and AppTest.java source files under a newly created my-app directory. Take a moment to step through the newly created files and directories.

Maven Goals
Similarly to how Ant has tasks, Maven uses goals. Maven has goals to clean, compile, test, and package a Java project.

If you created the my-app project above, cd to the my-app directory. Under the my-app directory execute the following command from the prompt or terminal.

mvn package

With the above command, Maven will compile, test, and jar the project. You should see a BUILD SUCCESSFUL. At the end of every single build, I look for that message in the prompt.

The most common command I execute is the clean compile.

mvn clean compile

One thing to note is that the project jar, compiled classes, and any other generated documents and files will be placed under the my-app\target directory.

Project Dependencies
An typical Java project will use external third party libraries. As an example, lets say our application makes good use of the Jakarta Commons-CLI library. We denote this dependency to Commons-CLI by adding the following configuration in the Project Object Model file, pom.xml.

<dependency>
  <groupId>commons-cli</groupId>
  <artifactId>commons-cli</artifactId>
  <version>1.0</version>
  <scope>compile</scope>
</dependency>

Many of the most popular and useful jars are hosted by Apache and/or Ibiblio. For jars hosted on these repository sites, Maven will discover them and download them. For jars not hosted on these repository sites, you will need to download them yourself and place them in a centralized location for Maven to discover.

Managing Multiple Projects
The one reason that I choose Maven, over other build tools such as Ant, is that it easily allows me to build multiple projects with a single command. Maven allows you to group multiple projects and define their dependencies. This feature allows you to create a Maven project for each logical component in your Java application.

The Maven documentation has a great tutorial, with the necessary XML configurations, to group two or more dependent projects. Once you have grouped multiple projects together you can compile, test, and/or package them with a single Maven goal.

Java 1.5
If your Java source files are written using Java 5 code, then you will need to update the build configuration in the Project Object Model file. Add the following bit of XML to your pom.xml.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
  </plugins>
</build>

If you are managing multiple projects, you can place the above XML in the parent pom.xml to affect all other child modules.

IDE Projects
Another great feature of Maven is that once you have defined you Maven projects and their inter-dependencies you can then create an IDE project. To create an Eclipse project execute the following command.

mvn eclipse:eclipse

For an IntelliJ IDEA project, try the following command.

mvn idea:idea

For a NetBeans project, try the following command.

mvn netbeans-freeform:generate-netbeans-project

Maven Repository
One thing to note is that Maven downloads a lot of jars and places them in the Maven repository. The Maven repository is located in your user home directory. In Windows, the Maven repository is located under C:\Documents and Settings\<username>\.m2. In Mac OS X, you can find the Maven repository under ~/.m2.

If you create a project for your favorite IDE you will need to add a M2_REPO variable to point to Maven repository. In Eclipse, to add the M2_REPO variable select Window > Preferences > Build Path > Classpath Variables. Add the new M2_REPO classpath variable and set it to the Maven repository folder location, C:\Documents and Settings\<username>\.m2\repository.

Technorati Tags: , , , , , , , , , , , ,