Category Archives: Mvn

Entering into Maven’s World

I was never so close to Maven, I mean, I used it in some projects in the past and never really liked it; I didn’t know it in deep and it kept bothering me downloading all required dependencies all the time (and I really do mean all the time!).
Well, regardless of my past experiences with it, I recently needed some tool to manage a not so huge but complex project. It is a project with “a few” dependencies, unit testing and so on and I was not in the mood for managing it manually. So I had a quick look into this kind of tools and found that Maven still looks like the most mature of them. After that, a convenience friendship between Maven and me arose and I decided to write a simple reference about using Maven (something that I still haven’t found).

Maven_logo

NOTE: This article doesn’t aim to be a reference on how to manage a project with Maven but a quick help to refresh one’s memories of Maven.

Installing Maven

This is a topic that you can easily find it in the Maven official documentation but I will tell you once again (just for the lazy ones).

Step 1

Download it! Yes! It would seem unusual but you need to download one of the latest stable versions of Maven to make it work!
You can download Maven from: http://maven.apache.org/download.html
Did you get it? Amazing! keep on working!

Step 2

Uncompress the package somewhere you can remember. You will need the complete path to those files in the next step.
NOTE: If you are going to install Maven for all users on your computer you will need to chose and appropriated folder (/opt, /usr/local…).

Step 3

Define some environment variables. You will need to add M2_HOME and M2 and modify your PATH to include M2.
In Linux you can do it on your user’s .bashrc file or system wide on /etc/environment or /etc/rc.local, it’s up to you.
For example, in your user’s .bashrc file you would need to add some lines like the ones below:

export M2_HOME=/path/to/apache-maven-3.0.4
export M2=$M2_HOME/bin
export PATH=$PATH:/$M2

Furthermore, you can define M2_OPTS and check that your JAVA_HOME is properly defined:

export M2_OPTS=”-Xms256m -Xmx1024m”
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

M2_OPTS is useful to configure Java VM parameters to run Maven and JAVA_HOME just points to your current Java VM installation.

NOTE: If you don’t have a Java VM installed you have to install it (it’s NOT an option you must have it, Maven-Java, Java-Maven you know… it’s like peanuts butter-breakfast, breakfast-peanuts butter). I recommend OpenJDK for version 7 and above (Oracle defined OpenJDK as Java SE 7 reference implementation[1]) and Sun/Oracle for versions 6 and below but it’s just a personal recommendation.

Step 4

Refresh your environment variables (reboot, source your files, log out and log in… whatever it works for you) and test that Maven is properly installed by executing:

$> mvn -version

And you will see something like:

Selection_005

Creating a project

Once Maven installed and configured you can create your brand new project. Migrating an existing non-Maven project to a Maven one is pretty much harder than starting from scratch so we will create a new project and see how things work.

Maven can create different kind of projects so, you will need to know which kind of project do you want to generate. If you want an standard Java project this command will be enough:

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.ixavi.java.simpleproject -DartifactId=maventest

If you are going to implement a web application you will need a different command:

mvn archetype:generate -DgroupId=com.ixavi.java.webproject -DartifactId=maventest-web -DarchetypeArtifactId=maven-archetype-webapp

Take into account that these commands creates a folder with your shiny new project, you don’t need to create a dedicated folder to execute these commands. Keep this in mind because almost all other Maven commands requires you to be on the same folder of the pom.xml file.

pom.xml and local reposiry

The pom.xml[3] file is responsible for describing the project structure, it’s the base of all Maven projects and let you control and version (since it’s an XML file) a lot of projects properties. In this file you will define dependencies, Maven plugins, extra repositories and so on. As I said before, almost all Maven commands should be executed from the pom’s folder otherwise commands would fail.

The local repository is the place where Maven will store all the installed artifacts. At the beginning you can think about it as a sorted set of folders for downloaded JARs (it has more functions but this is the most important at the moment) and you will be getting on with it while you keep on working with Maven.

In Linux systems, the local repository is often located in /home/User/.m2 in Win systems I think it’s in MyDocs/.m2 by default but I’m not sure.

Integrating the project into Eclipse

Maven has full support for Eclipse and generating a project descriptor for a Maven project is as easy as execute the command below:

mvn eclipse:eclipse

It will let you import this project into Eclipse from “Existing projects into workspace” but, unfortunately, it’s not enough. 

If you import a simple Java project just like I explained above you will get a lot of compilation errors; that’s because Eclipse doesn’t know where the local Maven repository is placed. To fix this issue you will need to add a variable on your classpath pointing to that repository. You can add this sort of variables on the Eclipse options dialog: Window > Preferences > Java > Build path > Classpath variables > Add. Just add a new variable called M2_REPO with the full path to the local repository as value.

There is a Maven plugin for Eclipse called M2Eclipse that you can install just by adding its site[2] into Eclipse’s software sources sites. I didn’t installed because I like to manage it using the command line but, once again: it’s totally up to you.

Implementing tests and running them

Maven has full support for unit testing, by default it creates a dedicated folder for test files and includes the JUnit dependency. Maven also generates a sample test on the same package of the main Java class (but under the test folder).

To execute all tests contained on this folder (those that are named appropriately) you just need to execute the command below and you will see a complete report at the end of these tests:

mvn test

NOTE: By default all test classes are called “WhateverTest” and extend TestCase (from JUnit).

I will come back with more about Maven but I think that it’s enough to get started :)


[1] https://blogs.oracle.com/henrik/entry/moving_to_openjdk_as_the
[2] http://download.eclipse.org/technology/m2e/releases
[3] POM stands for Project Object Model and you can find more information about this file on: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html