Maven

What it is and what it isn’t

Maven is first and probably most agreed on by developers a dependency manager. Public repositories all over the net house project dependencies, as well as files called poms which contain descriptors of other files that the dependency is dependent on. As a dev this can be quite a time saver so that all dependencies are downloaded and placed on the class path instead of manually downloading 1, finding out what jars its missing, then downloading those… so on and so forth.
Secondly maven is a build framework. Maven aims to be a constant for building any java project across all java projects. With a standard life cycle developers can become familiar with the framework and aim for a smoother build process. In order to do this Maven needs to be extend-able. Good thing it is. Maven has oodles of plug-ins for near any task for the build process. Plug-ins can (obviously) build java code, run ant tasks, run automated testing, run sql code, run code coverage reports, deploy artifacts (to a repository or a server), exec os code, pretty much the sky is the limit.

Installation.

Installing maven is not a complicated process but many get confused with it, mostly because of setting the proper environmental variables. First make sure you have java properly installed and the JAVA_HOME environmental variables properly set. Then download the latest version of Maven from http://maven.apache.org/download.html and unzip it wherever you please. From here you need to add a few environmental variables and add maven to the system path.

For Windows

Control Panel > System > Advanced > Environmental Variables
Under the system variables list add a new variable
MAVEN_HOME = C:\... (maven install location)
and append this to your path
;%MAVEN_HOME%bin\

For *nix

export M2_HOME=(Maven install location)
export M2=$M2_HOME/bin
PATH=$PATH:$M2

now at the command line you should be able to type
mvn --version
and get some kind of output like this…
Maven version: 2.0.9
Java version: 1.6.0_11
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"

How it works.

Maven uses a file called the POM (http://en.wikipedia.org/wiki/Project_Object_Model#Project_Object_Model), short for project object model. This is an XML file that describes your project. Here you can list what the name of your project is, what is its version, how it is packaged, how it is built, repositories for any dependencies, and last but not least its dependencies. There are many advanced configurations such as specifying profiles that are beyond the scope and necessity of this entry to go into.
Lets dig in shall we?

<?xml version="1.0" encoding="utf-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.weneck.webapplication</groupId>
<artifactId>WebApplication</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Sample Maven Webapp</name>
<url>http://maven.apache.org</url>
...

The pom starts out simple enough, you have your XML encoding and versioning information, project declaration and schema information. The modelVersion declared what version of the POM we are using, in this case 4.0.0, not to be confused with the version of your project. Next is the group id of your organization, the name of your artifact (artifactId), how your artifact should be packaged, in this case a WAR file (http://en.wikipedia.org/wiki/WAR_%28Sun_file_format%29). Finally a name and a version number of your artifact should be declared. Simple enough eh?

<repositories>
<repository>
<id>springsource maven repo</id>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>

The next section defines your repositories. Kinda like how it reads. In this case we are specifying springsource’s milestone repository so we can grab the Spring 3 RC artifacts (important for the final segment in this piece). You can also specify these in your settings.xml file locates in the maven home directory. This is usually ~/.m2/ or %USER_HOME%/.m2 depending on your OS.

<build>
<finalName>WebApplication</finalName>
<plugins>
<plugin>
....

The build section is next, here we define the plugins we are going to user, most commonly the compiler plugin, a properties file plugin if needed, and if you’re using eclipse (you should be) the maven-eclipse plugin. (fear not, full source attached at the end of my rant.

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
...

And finally the dependencies section. Here we define each of the dependencies our project is… well… dependent on. Easy peasy huh?

Now the cool part, putting it all together. So there is a common file structure maven expects, as well as few file we need to put in place for this thing to get off the groung. They are all included in the source, but I’ll gloss over them here. Maven expects a src folder with 2 folders within it, main and test. It’s not rocket science, as the names suggest you source code goes here. Inside the main folder goes your main files, and inside the test folder all files associated with your testing process/procedures. (you are testing your code right?) Inside main you have java, resources, and webapp. Inside java is your java code, resources is your resources, and webapp are the files needed for a webapp. Am I sounding redundant here? I also stuck in a nearly blank jsp for good measure. Hey, when its all done we want to see something other than a 404, amiright?

Using Maven

You can use maven from within eclipse, which used to work really well, but last time I tried was kinda buggy and bothersome. So I prefer the command line. First things first we need to configure eclipse to use all these wonderful dependencies that maven is managing for you.
mvn eclipse:eclipse
This line will download all your dependencies into that .m2 folder I referenced earlier. It will also write a .project and .classpath file for eclipse’s benefit. This will point java’s build path within eclipse to those dependencies which are not being stored within your project. Be sure to refresh the workspace after performing this command as eclipse does not activley monitor those files and will continue to complain about missing dependencies untill you do so.
mvn clean
This wonderful command performs a clean on your project, simple.
mvn install
Here’s our meat and potatoes. This command does what we really came here for. It creates the target directory structure, builds our code, and packages it all into our pre-defined package structure. In our case the war file. It also puts all the structure in place for eclipse’s WTP so if you have a java server configured in eclipse you can now add the project to the servers hosted projects. (Once added to the web server you dont need to continually build with maven, you can build with eclipse and WTP will pick up the changes and replace the necessary files in the web-server or prompt you for a restart.)
You can also chain these command together.
mvn clean install
That about wraps it up for Maven, but stay tuned for the next segment, Hibernate.

As promised Source

One Response to “Maven”
  1. [...] put it off time and time again because there was just so much area I wanted to cover. (see previous post) So finally I’ve decided to cut a bunch of scope and just get this [...]

  2.  
Leave a Reply

You must be logged in to post a comment. Login »