Maven basics

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 makes simpler the management of a Java project. All the processes becames more simple because you don’t need to know the details, but all can be done just using some plugins that you can choosed on several open source repositories.

With Maven all the project information are defined in a single point, the POM (Project Object Model), that is a simple xml file. We will see later more details on POM file.

Below is showed the Maven project structure:

my-app|--
 pom.xml`--
 src
 |-- main
 |   `-- java
 |       `-- com
 |           `-- mycompany
 |               `-- app
 |                   `-- App.java
 `-- test
	`-- java
		`-- com
			`-- mycompany
				`-- app
					`-- AppTest.java

Setting up Maven on your machine

Lets see what are the needed steps to install and configure Maven on your machine.

1. Download and install Maven

First of all you need to download a version o Apache Maven from here.
Then extract the zip archive somewhere on your local machine (e.g. “C:\Program Files\Apache Software Foundation\apache-maven-3.0.3″).

2. Define environment variables

Define the following three environment variables and then add them to the Path env variable:

M2_REPO = C:\Users\<YOUR USERNAME>\.m2\repository
MAVEN_HOME = C:\Program Files\Apache Software Foundation\apache-maven-3.0.3
MAVEN_OPTS = -Xms256m -Xmx512m -XX:MaxPermSize=256m

3. Install M2E plugin

At the end you need to install one of these two Maven plugin for Eclipse.
Go on “Help > Install New Software…” and paste one of the following URL:

  1. Eclipse: http://download.eclipse.org/technology/m2e/releases
  2. Sonatype: http://m2eclipse.sonatype.org/sites/m2e/

Personally I use the second one.
If you install the first one maybe you need to install also m2e-wtp from Eclipse Marketplace.

Compile the source

Below you can find a quick reference to the most used Maven command.

mvn compile
mvn install // compile and build the project packet (i.e. jar or war)
mvn clean // clean the project target folder
mvn install -Dmaven.test.skip.exec=true // as install but skip the test
mvn eclispe:eclipse

Maven properties

Build in properties

${basedir} represents the directory containing pom.xml ${version} equivalent to ${project.version} or ${pom.version}

Pom/Project properties

All elements in the pom.xml, can be referenced with the project. prefix or using pom. as prefix. This list is just an example of some commonly used elements.

${project.build.directory} results in the path to your “target” dir, this is the same as ${pom.project.build.directory}
${project.build.outputDirectory} results in the path to your “target/classes” dir
${project.name} or ${pom.name} refers to the name of the project
${project.version} or ${pom.version} refers to the version of the project
${project.build.finalName} refers to the final name of the file created when the built project is packaged

Local user settings

Similarly, values in the user’s settings.xml can be referenced using property names with settings. prefix.
${settings.localRepository} refers to the path of the user’s local repository

Environment variables

Environment variables can be referenced using the env prefix ${env.M2_HOME} returns the Maven2 installation path
${java.home} specifies the path to the current JRE_HOME environment use with relative paths to get, for example:
${java.home}../bin/java.exe

Java system properties

All Java System Properties defined by the JVM.

Custom properties in the POM

User defined properties in the pom.xml.


hello

${my.filter.value} will result in hello if you inserted the above XML fragment in your pom.xml

Parent Project variables

How can parent project variables be accessed?

You can use the prefix: ${project.parent}.

A good way to determine possible variables is to have a look directly at the API. I’m currently using Maven 2.2.1, and to access the Parent you can use ${project.parent}. This will return an org.apache.maven.project.MavenProject instance.

To access the parent version: ${parent.version}.

Reflection Properties

The pattern ${someX.someY.someZ} can simply sometimes mean getSomeX().getSomeY().getSomeZ(). Thus, properties such as ${project.build.directory} is translated to getProject().getBuild().getDirectory().

If you want to learn more retro games news, read our magazine and subscribe to our newsletter.