Search This Blog

Loading...

Monday, January 7, 2013

Maven: Getting started with Apache Maven 3.0.4 (Part 1)

What is maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Maven's Objectives
Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:
  • Making the build process easy.
  • Providing quality project information.
  • Providing guidelines for best practices development.
  • Allowing transparent migration to new features.

Providing quality project information
It also provides plenty of useful project information that is in part taken from your POM and in part generated from your project's sources. For example, Maven can provide:
  • Change log document created directly from source control.
  • Cross referenced sources.
  • Mailing lists.
  • Dependency list.
  • Unit test reports including coverage.

Providing guidelines for best practices in development and testing
Maven aims to gather current principles for best practices development, and make it easy to guide a project in that direction.

For example, specification, execution, and reporting of unit tests are part of the normal build cycle using Maven. Current unit testing best practices were used as guidelines:
  • Keeping your test source code in a separate, but parallel source tree.
  • Using test case naming conventions to locate and execute tests.
  • Have test cases setup their environment and don't rely on customizing the build for test preparation.
Maven also aims to assist in project workflow such as release management and issue tracking.

Maven also suggests some guidelines on how to layout your project's directory structure so that once you learn the layout you can easily navigate any other project that uses Maven and the same defaults.
For more information and a complete set of features about maven please see the references below.
Whatever the reasons that made you choose Maven (be it build automation, dependency management, standardization, testability, lifecycle management, continuous integration, or any other industry best practice), This and coming articles will get you up and running in the shortest time possible.
Typically we now have solid information about Maven. Let's dirt our hands with the installation process on both windows and Mac OS X as those are my main development operating systems. The Installation on different operating systems is not far from the provided steps below, enough talking and let's goes ahead with coding.

Prerequisite software pieces:
  1. JDK 1.5 or above to allow maven installation process goes successfully, not JRE because it is not sufficient.
  2. Apache Maven v3.0.4, and it could be downloaded from here (download).
Installing Maven on Windows and Mac OS X operating systems for the first time.

Here we will set up Maven on various platforms and host environments.

Setting up Apache Maven on Windows:
  1. Maven is a command line tool, and needs to be integrated with windows environment variables. It is very easy to setup and it is Java dependent.

  2. Download and unzip the distribution archive, i.e. apache-maven-3.0.4-bin.zip (as of this writing) to the directory you wish to install Maven 3.0.4. These instructions assume you chose C:\apache-maven-3.0.4 which will be created from the archive.

  3. Add the M3_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, and then adding the M3_HOME variable in the system variables with the value C:\apache-maven-3.0.4. Be sure to omit any quotation marks around the path even if it contains spaces.

  4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.

  5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M3_HOME%\bin to add Maven available in the command line.

  6. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.6.0_38 and that %JAVA_HOME%\bin is in your Path environment variable.

  7. See the below Figure 1 which summaries the steps from 3 to 6.




    Figure 1: Maven windows integration steps
  8. Go to verification step below to ensure that your maven installation goes successfully.

Setting up Apache Maven on Mac OS X:

Installing Maven on the Mac OS X isn’t very different from the installation and setup on Linux. This really shouldn’t be a surprise because OS X is built on top of BSD Linux in the first place.
  1. Extract the distribution archive, i.e. apache-maven-3.0.4-bin.tar.gz to the directory you wish to install Maven 3.0.4. These instructions assume you chose /Applications/Maven_3.0.4 which will be created from the archive.

  2. The next step is to add commands to export the PATH and M3_HOME environment variables in the .bash_login file.

  3. In a command terminal, add the M3_HOME environment variable, e.g.

  4. Optional: Add the MAVEN_OPTS environment variable to specify JVM properties, e.g.

    This environment variable can be used to supply extra options to Maven.

  5. Add M3_HOME/bin environment variable to your path, e.g.

  6. Make sure that JAVA_HOME is set to the location of your JDK, e.g.
    export JAVA_HOME=/usr/java/jdk1.7.0_10 and that $JAVA_HOME/bin is in your PATH environment variable.

  7. Go to verification step below to ensure that your maven installation goes successfully.
Verify the Maven installation:
  1. From Windows, open a new command prompt (Winkey + R then type cmd).

  2. From Mac, open command terminal.

  3. Run

    To verify that it is correctly installed.

  4. You should see something like the following Figure 2, otherwise check the installation steps:


    Figure 2: Maven installation is successful
Apache Maven is now ready and available for use in the command line. It is also available for integration with IDEs and other development tools.

By reaching here you are done the installation and verification steps successfully, congratulations. Next I will explain how to create, compile and test a project.

References:
  1. http://maven.apache.org/
  2. http://maven.apache.org/maven-features.html
  3. http://maven.apache.org/download.cgi