02 - Maven Installation

2. Installation

2.1 Downloading Maven

2.2 Installing Maven

2.2.1 Installing Maven on Mac OSX

2.2.2 Installing Maven on Microsoft Windows

2.2.3 Installing Maven on Linux

2.3 Create new project with Maven

2.4 Testing a Maven Installation

2.5 Uninstalling Maven

2.6 Maven properties

2.6.1 Maven settings

2.6.2 Environment setup

2.6.3 Project properties

2.6.4 System properties

2.6.5 Resource filtering

2.7 Maven configuration

⁃ 2.7.1 Plugins Configuration

 

 

2.1 Dowloading Maven

Maven can be downloaded from:

http://maven.apache.org/download.html

The archive must be chosen according to the platform.

Being a Java tool this it has to be installed first.

2.2 Installing Maven

2.2.1 Installing Maven on Mac OSX

Because Maven is a command-line tool it needs to be extracted and configured with OS environment.

In order to proceed with the installation first it has to be checked the Java Development Kit availability. Try it with the following command line

java -version

The following output will be shown:

 

 

The PATH and M2_HOME environment variables have to be exported in the .bash_login file

export M2_HOME=/usr/local/maven 
export PATH=${PATH}:${M2_HOME}/bin

 

The /usr/local/maven is an example directory where the maven archive can be extracted.

2.2.2 Installing Maven on Microsoft Windows

Installing Maven on Windows has some differences regarding the location of the installation and the environment variable setting. The environment variable should be configured properly by setting the installation directory. After unpacking Maven archive the environment variables PATH and M2_HOME have to be set. It can be done from command line:

For example:

C:\Users\Some user >  set M2_HOME=c: \ Program Files \ apache - maven - 3.2.3
C:\Users\Some user >  set PATH=%PATH% ; %M2_HOME% \bin

2.2.3 Installing Maven on Linux

For installing Maven on Linux the same procedure for installing Maven on Mac OSX has to be followed

2.3 Create new project with Maven

Choose a folder where a project can be created. Then type in command line the following commands:

Some downloads will take place in command line and a list with archetypes with number, name and description.

The command prompt asks for the version of the archetype. Let’s choose default as being the last stable version of the archetype.

………

………

Then the command prompt will ask for the following naming conventions:

GroupId

This identifier will identify the project uniquely across all the projects. In case of multiple module project a new name should be added to the parent’s groupId as root package. In Maven repository this name represents the hierarchical location.

ArtifactId

The identifier represents the name of the jar without version. This identifier can have whatever name but with lowercase letters and no strange symbols.

Version

If the project is distributed the version should be one typical with numbers and dots, e.g.

1.0

1.1

1.0.1

After completion the following message will be displayed:

This project was created in a folder manually with a manual pom. The main folder contains subfolders for the application and test source code. It contains all the package structure and a test case. The structure directory is the following (with cmd - tree ):

or in Windows Explorer :

 

If the project is not generated in interactive mode then the naming conventions can be specified as arguments in the same command with archetype generate.

C:\mvn archetype:generate
-DgroupId=com.company.insurance
-DartifactId=carAccident
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

The following output will be displayed in the console:

The project will have the following structure:

 

 

 

 

 

 

 

 

 

 

 

 

 

In order to get a trace of the operations in the console at the main command should contain

-X argument.

2.4 Testing a Maven Installation

After the Maven is installed its version can be checked by running

1. on Windows :

     mvn -v or mvn—version

from command-line. If Maven has been installed, the result will be like:

If the output is not like this the operating system cannot find the mvn command and it should be checked if the PATH environment variable and M2_HOME are properly set.

2. on Mac

    mvn -version

The output will be the following:

Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T23:58:10+03:00)
Maven home: /Users/username/Documents/apache-maven-3.2.3
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platformencoding: MacRoman
OS name: "mac os x", version: "10.10", arch: "x86_64", family: "mac"

 

2.5 Uninstalling Maven

If Maven has to be removed from the computer then the installation directory has to be deleted and also the environment variables have to be removed. The ~/.m2 directory has to be deleted if contains the local repository.

2.6 Maven properties

Maven properties can be defined in pom.xml file or in any resource that will be processed by the Maven Resource plugin’s filtering features.

Properties have the syntax: ${ and}.

The implicit properties for a Maven project are:

project.*

This prefix can be used to reference values in a Maven POM.

settings.*

This prefix can be used to reference values from Maven Settings in ~/.m2/ settings.xml

env.*

This prefix can be used to reference environment variables ( PATH and M2_HOME)

 

2.6.1 Maven settings

Maven settings.xml file is used to define some values for configuration of the local repository location, alternate remote repository servers, user configuration as authentication information for private users, active build profiles.

There are two settings files located at:

$M2_HOME/conf/settings.xml
${user.home}/.m2/settings.xml

The files are optional. If both files are present, the values in the Maven installation settings file are overriden by the user home settings.

When the file settings.xml is located in root directory of an application it will be automatically used to configure Maven at compile time.

If the settings.xml is not located in the root directory of the application or if the settings configurations are changing often this file should be put in a custom location.

Top level elements of the settings.xml:

localRepository

The location of the local repository can be changed by the user configuration. The default value is

${user.home}/.m2/repository/

In settings.xml is configured :

<settings>
…..
    <localRepository>/path/to/local/repo/</localRepository>
…..
</settings>

The local repository must have an absolute path.

interactiveMode

This variable should be true if Maven should interact with the user for input and false if not ( default is true)

usePluginRegistry

The variable is true if Maven should use

${user.home}/.m2/plugin-registry.xml file to manage plugin versions, defaults to false.

offline

true the build system should operate in offline mode, defaults to false. This variable is used In case of build servers cannot connect to the remote repository or because of security reasons of network setup.

2.6.2 Environment setup

In order to use Maven for automatic resolution of transitive dependencies for software projects and dependency resolution on a Windows it is necessary to set proper Windows Environment Variables. The steps that have to be done are similar for all versions of Windows.

Step 1

Right-click on the Computer from Start Menu and choose Properties.

Step 2

Click Advanced system settings in the left side of the System window.

Step 3

In the window System Properties click Environment Variables button

.

Step4

In System Variables part of the Environment Variables window choose Path variable and click Edit.

Step 5

In the Edit System Variable window that appears, move the cursor to the end of the Variable value field. Type semicolon; and enter the path to the Maven bin directory in this case

C:\apache-maven-3.2.2\bin. Click OK.

In order to test that the Path environment variable has been correctly set up you can test it by applying the steps form the 2.4 section from this tutorial.

2.6.3 Project properties

The Maven Project Property is referenced through the property name from Maven Project Object Model (POM).

Anything in Maven POM can be referenced with a property. For example:

project.name and project.description

The project name and project description are used to reference from documentation.

project.groupId and project.version

If the projects are build in a multi - module build type they share the same groupId and version identifiers.

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>main-project</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

project.artifactId

This property is used as the name of the deliverable project. In case of generating a WAR file without version identifier then project.artifactId should be referenced.

<build>
<finalName>${project.artifactId}</finalName>
</build>

 

2.6.4 System properties

Maven uses all the properties from java.lang.System. In a Maven property it can be referenced all can be retrieved with System.getProperty().

Examples

java.version Java Runtime Environment version

java.home Java installation directory

java.class.pathJava class path

2.6.5 Resource filtering

One important feature of Maven is Resource filtering. By simply adding the file with resources in the location src/main/resources Maven will add Java properties file in the project.

Maven can use in the resources files variables and their values can be changed in the process - resources phase. For example if we consider the file :

src/main/resources/application.properties

with the variables:

# application.properties
app.version=${version}
application.name=${project.name}

add the following lines to the pom.xml file.


The application.properties file will be filtered by Maven during the prepare - resource phase. The file will be filled with values and then placed in the target directory with the following content indicated by ${...} changed :

# application.properties
app.version= 0.0.1- SNAPSHOT
application.name=Maven Archetype

Maven can be instructed also to filter only some files / directories and to not filter others by defining at least one <resource> element.

To avoid binary files to be filtered with the exclusion has to be added to the filtering and another entry with the resource and the filtering disabled has to be added again.

2.7 Maven configuration

After the Maven is installed it could be noticed that some local user-specific files have been created and a local repository in the home directory.

The ~/.m2 home directory there will be:

~/m2/settings.xml

See the 2.6.1 section where this file is described

~/m2/repository

This directory contains local Maven repository. When a dependency is downloaded from the Maven repository a copy of this is stored in the local repository.

2.7.1 Plugins Configuration

Plugins can be customized by configuring them in the project’s POM by the properties 

defined by goals in a plugin.

For example for the compile goal there are more configuration parameters like

source, target, compilerArgument, fork, optimize, etc.

Maven Help Plugin can be used to describe a particular plugin goal if a plugin goal

configuration parameter is searched.

With the command mvnhelp:describe a particular plugin can be described from the

command line.

$ mvn help: describe -Dcmd=compiler:compile -Ddetail

-Ddetail argument will determine Maven to print all of the goals parameters for the

entire plugin.

Like us on Facebook