Learning Maven

A colleague at work (hi Clemens! :)) has shown me the wonders of Maven, it’s a really cool tool to standardise the way Java projects are compiled, tested, packaged and distributed and also provides a great way to standardise which libraries and versions are used and downloads them automatically for you.


As everything with my blog, this is more for my future self-reference than to teach anybody anything, as I’m learning it as I go myself. Here’s what I’ve done

  1. Installing Java
    1. sudo apt-get install python-software-properties
    2. sudo add-apt-repository ppa:webupd8team/java
    3. sudo apt-get update
    4. sudo apt-get install oracle-java7-installer
    5. create $JAVA_HOME environment variable
      1. get path to java
        1. sudo update-alternatives –config java
        2. remove the trailing /jre/bin/java, for example if update-alternatives returns:
          /usr/lib/jvm/java-7-oracle/jre/bin/java, then the path for JAVA_HOME is /usr/lib/jvm/java-7-oracle/
      2. add JAVA_HOME=”YOUR_PATH” to /etc/evironment
      3. source /etc/environment to reload the file
      4. java -version should now work
  2. Installing maven:
    1. wget http://www-us.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
    2. tar -zxvf apache-maven-3.3.9-bin.tar.gz
    3. add the apache-maven-3.3.9/bin directory to the PATH environment variable by editing /etc/environment
    4. source /etc/environment to reload the file
    5. mvn -v should now work
  3. Creating a test maven project using an archetype. An archetype is just a template to create the folders structure and the default pom.xls file
    1. mkdir testmaven1
    2. cd testmaven1
    3. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    4. this will create a folders structure and a pom.xml file
  4. Adapting the pom.xml file
    1. The pom.xml defines what maven will do, through the following configurations
    2. These variables define your application
      1. <groupId>com.mycompany.app
        <packaging>jar <version>1.0-SNAPSHOT
    3. To define the modules and versions that will be used, use the block
      1. <dependencies>
    4. In order to find the details of the dependency you can use mvnrepository.com, for example to figure out the details for the apache POI library you can visit: https://mvnrepository.com/artifact/org.apache.poi/poi/3.15
    5. Maven has a lot of plugins. Plugins are Mojos (Like Pojos, but Maven plain Old Java Object) which can be executed when running different goals.
      1. <build>
  5. Executing different steps in the  Build Lifecycle
    1. If you want to compile, test and package your java project for example, it’s as easy as running mvn package.
      1. validate – validate the project is correct and all necessary information is available
      2. compile – compile the source code of the project
      3. test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
      4. package – take the compiled code and package it in its distributable format, such as a JAR.
      5. verify – run any checks on results of integration tests to ensure quality criteria are met
      6. install – install the package into the local repository, for use as a dependency in other projects locally
      7. deploy – done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s