Skip to content

Maven

Maven is not only a build tool. it can do a lot more, but i tend to use it for that mostly so that is what this focuses on.

A simple quick guide can be found here

installation

As always i first attempt to take the simple apt-get, so

install
apt-get install maven
mvn -v

creating a project

Maven has a repository packed with possible solutions for problems, and mvn generate will list them all for you :

generate archetype
mvn archetype:generate

Try it, at the time of writing it went all the way down to 839 solutions. And after that it suggests that i choose # 336 :

error
336: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

Why not, and why not take the default in the next options as well, it suggests the newest version. But after that you need to provide some real input.

  • groupId : this is a unique name, and resolves to a working group. Maybe a department of a company. The quick guide's example is -DgroupId=com.mycompany.app. So i will choose for org.klopt.sandbox.
  • artifactId : this resolves roughly to a module or application name. The quick guide named this -DartifactId=my-app. So i called it maventest
  • version : a string representing the version. hey man, i just started out so 0.1 for me
  • package : maven already fills in the groupId here, so i will go with that : org.klopt.sandbox

After a final confirmation, you will have a directory structure called /maventest'. It looks like this :

maventest/
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── klopt
    │               └── sandbox
    │                   └── App.java
    └── test
        └── java
            └── org
                └── klopt
                    └── sandbox
                        └── AppTest.java

Pivot in this is the pom.xml file. It looks like this :

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.klopt.sandbox</groupId>
  <artifactId>maventest</artifactId>
  <version>0.1</version>
  <packaging>jar</packaging>

  <name>maventest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

To use it you can now run 'goals' using this file. Since we installed 336: A sample application, also your source files App.java And AppTest.java contains some working code, albeit minimal :

example
package org.klopt.sandbox;

/**
* Hello world!
*
*/
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

This is the main file App.java. AppTest.java is used for unit testing and contains some more functions. But to build them both, run :

maven package
mvn package

It will create a new tree called 'target' which contains your newly built classes. Run with :

run command
java -cp target/maventest-0.1.jar org.klopt.sandbox.App

Luckily bash command completion (tab) helps with the class hierarchy string.

** maven goals / phases

The 'mvn package' command is called a goal, or phase or life cycle. Here are a couple more useful goals.

clean
mvn clean

Removes the target tree and returns to the begin state.

compile
mvn compile
java -cp target/classes org.klopt.sandbox.App 

Just generates the App.class tree, so target.classes.org.klopt.sandbox.App.class.

test
mvn test

Runs the tests in AppTest.java and also builds that first if needed.

install
mvn install

Install the package into the local repository, for use as a dependency in other projects locally. In my case it ended up in "/home/kees/.m2/repository/"

deploy
mvn deploy

Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

site
mvn site
google-chrome target/site/index.html

Nice one: generates a website containing the documentation for instance. The site will be under 'target/site'.

For a lot of these, you will need to have some patience the first time, because everything needed is downloaded at first use.