Introduction to Maven – Part 1

You’ve probably all heard of Maven and some of you are probably already using it. If not however, here’s a brief introduction into Maven and how to use it.

What is Maven?

Maven is a project management build tool that helps maintain builds, documentation and other project artefacts, which we’ll not cover here. Maven describes itself as

an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.

That may sound a bit of a mouthful, but essentially it means that Maven is a best practice build and management tool.

So, why should we use Maven instead of Ant?

  • In a typical project, Maven will require much less XML configuration than a corresponding Ant script. Ant requires build scripts to be generated whereas Maven defines the project within a pom.xml file – the Project Object Model.
  • Maven manages dependencies so you don’t manually have to download Jars and ensure they are the correct version. Maven downloads Jars from its repository – you specify what dependencies your application has and let Maven do the rest.
  • Maven provides templates for many different application types (e.g. Web App, EAR application etc.). You no longer have to worry about the layout of your application or how to generate Ant scripts.

Getting and Installing Maven

Downloading and installing Maven is a straightforward task. To install Maven you need to download it from the project site, unzip it to your hard drive and then set up two environment variables:

  • M2_HOME must point to the directory that you unzipped Maven into.
  • M2 should be set to %M2_HOME%/bin and then added to your path. To check that Maven is installed correctly, execute the command mvn –version from a command prompt. If all goes well, you should get the Maven version number returned to you.
~ > mvn --version
Maven version: 2.0.9
Java version: 1.5.0_16
OS name: "mac os x" version: "10.4.11" arch: "i386" Family: "unix"