Maven 2.0: Compile, Test, Run, Deploy, and Moreby Chris Hardin
The hardest part of getting started with a Java application is, well, getting started. So many logistical decisions have to be made up front. Where should the Java source files go? Where do I place unit testing? Where will we store dependency .jars? How will the project be built, documented, tested, and deployed? The choices made at this stage will follow a developer for the rest of the project. It's up to you whether those choices will haunt you or prove you to be a master Java architect later on. We'll assume the latter is the goal we are striving for, and now we just need a starting point.
There are many tools out there for building a Java project, including Ant. Ant has been on the top of many a developer's list as the revolutionary tool that got them out of the world of
make. For those of you not familiar with
make, it'll be enough t say that it just isn't the best tool to use for building Java projects, since it isn't platform-independent and it isn't that easy to use. Ant came along and changed all that by providing a platform-independent tool that uses an XML configuration file, the infamous build.xml. Ant has enjoyed heavy popularity with its many advantages, but it also has some drawbacks. The build.xml files can be extremely terse, and their use requires the developer to learn the syntax up front. While the learning curve isn't too steep, a Java developer's time could be better spent doing, well, development.
Maven is the new kid on the block, much like Ant was just a few short years ago. Maven 1.0 has been around for a few years and it was accepted by a wide audience of developers as an Ant replacement, but it offered very little relief from the old Ant build.xml file. Maven 1.0 was slow and clunky and using it was almost as difficult as getting started on a project with Ant. In fact, it was Ant at its core, and after an almost complete rewrite, Maven 2.0 was born.
The Benefits of Maven 2.0
The benefits of Maven 2.0 are numerous, as it does more than merely build your projects. If you are just starting a new Java project and you need to get started fast, Maven 2.0 will have you up an running in minutes. The following are some of the advantages of Maven 2.0:
- Standardized project layout and project structure generator.
- Standardized dependency-management mechanism.
- Multiple project support.
- Instant downloads of new plugins and features as the developer needs them.
- Website generation for up-to-date project information.
- Integration with source control: CVS and Subversion.
The list above is just a short list of the features available in Maven 2.0. These alone make Maven 2.0 a solid choice for a build management system. Now that we know what Maven is, let's look at how to get started.
The first thing we want to do is set up our directory structure. Wait--there is no need to do it by hand. Maven can do it for you, depending on the type of project that you are developing. Once you have downloaded and extracted the latest distribution of Maven 2.0, you should add the bin directory of the Maven distribution to your system path. You can run
mvn -version to test your installation.
Now that we have the tool installed, let's look at the example of creating a simple Java project. Maven uses archetypes to determine how the directory structure will be laid out. There are several built-in archetypes or you can write one of your own.
mvn archetype:create -DgroupId=com.oreilly -DartifactId=my-app
Voila! We now have our project layout.
my-app ----src ----main - ----java - ----com - ----oreilly ----test ----java ----com ----oreilly
Yes, it's that easy. It should be noted that this directory structure can be overridden by creating a new archetype, but deviating from the structure is not recommended, since one of the benefits of Maven is the standard directory structure. The directory structure contains two source trees: one for your Java application source code and one for your unit test code. You may also have noticed that the first time you ran Maven, it did some downloading. Maven will update itself with the appropriate functionality based on what plugin you use when you invoke the tool. Maven, by default, will get its updates from the Ibiblio repository. You can override Maven's choice of a remote repository in the conf directory of the Maven distribution or in the project itself.
You should also have noticed that Maven created a pom.xml file in the my-app directory. This is the meat and potatoes of your project. The pom.xml file is a set of instructions for Maven that tells it how to build the project and includes other special instructions. (POM is an acronym for "project object model.") By default, Maven also includes the JUnit dependency to encourage unit testing.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oreilly</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>