Friday, October 3, 2008

Maven: The coolest build tool ever!

I've been through the grinder on build tools: typing in VIC20 BASIC, building from the command line, building from .sh scripts, building from Make, building from Xmake, building from IDEs, building from nant.

It's been a glorious twenty-eight years of software development (including grade school years of software development). Now I'm hearing good things about maven. A few people love it; many don't like it. I've stayed away from the arguments and kept clinging to my Nant and Eclipse build tools, but now I've landed on a project that's using maven 2.0.9. I have an excuse to dive in!
And I really love it!

It was tough at first, but when I look at the time I dumped into learning Make and its implied rules, it really wasn't so bad. The documentation on "the internets" (thank you Mr. Bush) is pretty minimal, maybe enough if you already understand the big concepts. There is a new book out this month that looks promising: Maven: The Definitive Guide With only the Internet, I probably would have stayed a frustrated Maven user except a smart cookie named Jeff Ramsdale gave a colleague and I a thirty minute intro. To give back to the internets and to put my notes on maven somewhere, here is my high level understanding of organizing a maven project. I'm sure you'll correct me by commenting if I'm out in the weeds.


Maven drives everything via a lifecycle. This lifecycle is built into maven:
  1. validate - validate the project is "correct" and all necessary information is available.  Said another way, "go get my dependencies."
  2. compile - compile the source code of the project
  3. test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed. Don't mix your system tests here.
  4. package - take the compiled code and package it in its distributable format, such as a JAR.
  5. integration-test - process and deploy the package if necessary into an environment where integration tests can be run. Don't put unit tests here as unit tests != integration tests. These tests should be testing that the system or subsystem works.
  6. verify - run any checks to verify the package is valid and meets quality criteria
  7. install - install the package into the local repository, for use as a dependency in other projects locally
  8. deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
A portion of this lifecycle is called a phase, so the above is a list of phases. Maven has a configuration file called a pom.xml. When you invoke maven: $mvn, it looks at the nearest pom.xml and by default, it uses the "install" lifecycle. Since install is number 7 in the above list, maven goes through phases 1, 2, 3,...,7 and may perform operations based on what is in the pom.xml. You can also do a $mvn deploy and mention the phase right in the command line.


Profiles are an abstraction layer on top of the lifecyle. Creating a profile allows you to create configurations which affect how things are done. For example, on our project, we have some integration tests which take two hours to run and we have two integration tests that take ten seconds to run. Before we check in, we want to run those quick integration tests but not the long ones. So we explicitly named the two integration tests in the configuration for the surefire plugin (a junit test runner in maven) which runs the two quick integration tests in the integration_test phase when $mvn is executed.

For the slow integration tests, we created a profile "all_integration_tests" which re-configures the surefire plugin to include running the slower integration tests too (using the naming convention of * when $mvn -pall_integration_tests is executed.


Goals are like arguments you use to tell a plug-in what to do. By the way, everything in maven is a plug-in so plug-in's are first class citizens. A goal is an argument that you pass to the plug-in. I don't have more to say about goals because I haven't had to use them much. :-)

After all, gotta save something for later.

No comments:

Post a Comment