Maven
Maven
[src - https://maven.apache.org/download.cgi#Installation ]
Prerequisite - Make sure that JAVA_HOME exists and it is set to the location of your JDK.
WIN - JAVA_HOME - C:\Program Files\Java\jdk1.7.0_51
Unix - export JAVA_HOME=/usr/java/jdk1.7.0_51
Unzip the distribution archive to the desired directory (MAVEN_HOME).
apache-maven-3.2.5-bin.zip
apache-maven-3.3.3-bin.tar.gz
Add the unpacked distribution's bin directory to your user PATH environment variable.
PATH - C:\Program Files\Apache Software Foundation\apache-maven-3.3.3\bin
export PATH=$PATH:/usr/local/apache-maven/apache-maven-3.3.3/bin
Optional: Add the MAVEN_OPTS environment variable to specify JVM properties
in the user variables JVM properties as -Xms256m -Xmx512m
export MAVEN_OPTS="-Xms256m -Xmx512m".
Optional configuration
Maven will work for most tasks with the above configuration, however if you have any environmental
specific configuration outside of individual projects then you will need to configure settings. The
following sections refer to what is available.
Settings
Maven has a settings file located in the Maven installation and/or user home directory that configure
environmental specifics such as:
Security
As of Maven 2.1.0+, you can encrypt passwords in your settings file, however you must first configure a
master password. For more information on both server passwords and the master password, see the
Guide to Password Encryption.
Toolchains
As of Maven 2.0.9+, you can build a project using a specific version of JDK independent from the one
Maven is running with. For more information, see the Guide to Using Toolchains.
mvn –X - The switch activates debug logging. It says about different locations Maven reads settings.xml.
C:\Project\Maven\my-app>mvn -X
The POM contains information about the project and various configuration details used by Maven to
build the project(s).
It is an XML file.
It is fundamental Unit of Work in Maven.
It always resides in the base directory of the project as pom.xml.
There should be a single POM file for each project.
POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the
current directory.
It reads the POM, gets the needed configuration information, and then executes the goal.
Maven pom.xml is also not required to be written manually. Maven provides numerous archetype
plugins to create projects which in order create the project structure and pom.xml.
Before creating a POM, we should first decide the project group (groupId), its name(artifactId) and its
version as these attributes help in uniquely identifying the project in repository.
com.company.bank:consumer-banking:1.1.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
</project>
Super POM
This base POM is known as the Super POM, and contains values inherited by default.
Maven use the effective pom (configuration from super pom plus project configuration) to execute
relevant goal.
An easy way to look at the default configurations of the super POM is by running the following
command:
Now open command console, go the folder containing pom.xml and execute the following mvn
command.
Maven repository
A maven repository is a place (directory) where all the project jars, library jar, plugins or any other
project specific artifacts are stored and can be used by Maven easily.
USER_HOME
Unix/Mac OS X – ~/.m2
Windows – C:\Documents and Settings\{your-username}\.m2
o C:\Users\Samrat\.m2\repository
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.companyname.common-lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependencies>
<repositories>
<repository>
<id>companyname.lib1</id>
<url>http://download.companyname.org/maven2/lib1</url>
</repository>
<repository>
<id>companyname.lib2</id>
<url>http://download.companyname.org/maven2/lib2</url>
</repository>
</repositories>
</project>
External Dependency
It is very usual to have your own library specific to project containing jars which may not be available in
any repository for maven to download from.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
<dependencies>
</project>
External dependencies (library jar location) can be configured in pom.xml in same way as other
dependencies.
A plugin generally provides a set of goals and which can be executed using following syntax:
mvn [plugin-name]:[goal-name]
mvncompiler:compile
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Key concepts:
Plugins are specified in pom.xml using plugins element.
Each plugin can have multiple goals.
You can define phase from where plugin should starts its processing using its phase element.
We've used clean phase.
You can configure tasks to be executed by binding them to goals of plugin. We've bound echo
task with run goal of maven-antrun-plugin.
That's it, Maven will handle the rest. Maven will download the plugin if not available in local
repository