Sunday, December 23, 2012

Apache Maven (I)

Today I'm gonna talk about Apache Maven. Essentially, it is a framework to automate build process of Java based projects.  I choose to start with this tool because in my opinion,it's the base for a healthy project development and code management.

Maven was born as an attempt to simplify the build process in the Jakarta Turbine project. Turbine used Ant tools on its build process. Projects managers realized that they had to handle with different build descriptors that generated a lot of JARs. Finally, these JARs were loaded into CVS. They developed a tool to help to standardize all these build descriptors using a clear definition of what the project consisted of, and provided a way to publish project information. Last, they also supplied a mechanism to distribute and share the output packages (jars,wars, ...), to be used in other projects.

That project was successful, and now it's on its 3.0.4 version. It can be used to build any project written in Java. It's a very mature product with an awesome support, and a lot of extensions that make very easy to manage all the build process. Developers around the world adopted this tool in his day-to-day work.


Maven philosophy

Before to start working with Maven, I would like to stop to reflect which are its principal goals. We should understand that the main reason of Maven existence is helping developers to reduce waste on the build process. It achieves that goal making the build process easy using project descriptors instead a set of tasks. It employs an uniform build system along all the source code tree. All these descriptors provide a high quality and valuable information about its structure, build workflow and dependencies. Using Maven implicitly implies to work using development best practices guidelines, such using standardized paths names,  project structure and information.


Start working

My intention in this post is to describe the steps to install and build a minimal project. I admit that this example seems not to contribute in a very effective way to reduce waste on building process, but you will realize of that reduction as you go along the following blog posts.

Install Java development kit

These steps can be reproduced to install Maven on a Linux system, but are very similar in other OS. First of all, we must install Java Development Kit. You can download it from Oracle Java webpage:

It's important to know which architecture implements your OS before downloading an specific version. If you don't know your version, you can execute in a terminal the command 'uname -a'

In my case, I'm runing a Fedora 16 x64, then I choose 'jdk-7u10-linux-x64.tar.gz'

Once you have it, you can decompres in a directory.

cd ~/Downloads
mkdir -p ~/javaprojects/bin
tar -C ~/javaprojects/bin -z -xvf

Install Maven

Choose the latest version in tar.gz format. Now is the 3.0.4.
cd ~/Downloads
tar  -C ~/javaprojects/bin -z -xvf apache-maven-bin-3.0.4.tar.gz

Add the following lines to your ~/.bashrc file:

export JAVA_HOME=~/javaprojects/bin/
export MAVEN_HOME=~/javaprojects/bin

Now, open a new terminal an execute the command:

mvn -version

You should see an output similar to this:

Maven home: /home/user/javaprojects/bin/apache-maven-3.0.4
Java version: 1.7.0_10, vendor: Oracle Corporation
Java home: /home/user/javaprojects/bin/jdk1.7.0_10/jre
Default locale: ca_ES, platform encoding: UTF-8
OS name: "linux", version: "", arch: "i386", family: "unix"

Create the first project

As many other tutorials, we will start implementing a simple Java program that will print "Hello world" to the standard output. Let's start creating a directory to your new project.

mkdir ~/javaprojects/helloworld
cd ~/javaprojects/helloworld

Now, you can create a directory that will store your first class.

mkdir -p ~/javaprojects/helloworld/src/main/java/com/example

As you can see , I will put the code on 'src/main/java' . It's the path used to store all Java code files. From here, you will use the corresponding directory path depending on the package you will use in your class. In my case, it will be packaged in com.example, so I create a new file in:


Write the class:

package com.example;

public class HelloWorld{
   public static void main(String args[]){
      System.out.println("Hello World!");
Now comes the interesting step. Writing the project descriptor. Maven use xml files called 'pom.xml'  to describe the project. This name comes from "Project Object Model". Maven will read it to obtain its information it in order to build the project properly.

In that case , we only need a minimal 'pom.xml' . You must create the 'pom.xml' file, on '~/javaprojects/helloworld/', and fill it with the following content:

 That's all. We have finished to set up our first project. You can observe that there are three obligatory elements:
  • artifactId: Your project identifier.
  • groupId: A group identifier used to group your projects.
  • version: Version number of your project build.

How to build

In the following posts, we will pay more attention to building phases, but at this point we can use some of them:
  • mvn clean : Cleans all data generated in previous builds.
  • mvn compile : Compiles the project.
  • mvn package : Packages the project. Depending on the project type it will generate a type of output. By default generates a JAR.
  • mvn install : Installs the output package and the information project data on your local repository.
  • mvn deploy : Deploys the output package and project information on a remote repository.
  • mvn help:describe -Dcmd=install : Shows help about commands. In that case, it shows help about 'install' command.
If you come back to your project and execute 'mvn compile', you will observe that at the end of the building process, you will obtain a new directory called 'target'. This is the directory used by Maven to generate all the outputs. In your case, in that directory you will find a subdirectory with all compiled classes, 'classes/com/example/HelloWorld.class'. You can try your app going to 'target/classes', and executing the command:

java com.example.HelloWorld

On the next post I will talk about POMs.

No comments:

Post a Comment