0% found this document useful (0 votes)
49 views11 pages

Maven

The document provides instructions on installing Maven and configuring the environment. It discusses setting JAVA_HOME and adding Maven's bin directory to the PATH. It also covers optional Maven configuration settings like MAVEN_OPTS and configuring proxies, passwords and toolchains.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views11 pages

Maven

The document provides instructions on installing Maven and configuring the environment. It discusses setting JAVA_HOME and adding Maven's bin directory to the PATH. It also covers optional Maven configuration settings like MAVEN_OPTS and configuring proxies, passwords and toolchains.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Installation Instructions

[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".

This environment variable can be used to supply extra options to Maven.

Run mvn --version to verify that it is correctly installed.

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:

 HTTP proxy server


 repository manager location
 server authentication and passwords
 other configuration properties
 For information on this file,

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

Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)


Maven home: C:\Manish\Appl_Setups\apache-maven-3.2.5\bin\..
Java version: 1.8.0_11, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_11\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from C:\Manish\Appl_Setups\apache-maven-
3.2.5\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\Samrat\.m2\settings.xml
[DEBUG] Using local repository at C:\Users\Samrat\.m2\repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for
C:\Users\Samrat\.m2\repository
[INFO] Scanning for projects...
[DEBUG] Extension realms for project com.mycompany.app:my-app:jar:1.0-SNAPSHOT:(none)
[DEBUG] Looking up lifecyle mappings for packaging jar from ClassRealm[plexus.core, parent: null]
[INFO] ------------------------------------------------------------------------
------------------------------------------------------------------------
POM (Project Object Model)

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.

 groupId This is an Id of project's group.


This is generally unique amongst an organization or a project.
 artifactId This is an Id of the project. This is generally name of the project.
Along with the groupId, the artifactId defines the artifact's location within the
repository.
 version This is the version of the project.
Along with the groupId, It is used within an artifact's repository to separate versions
from each other.

For example: com.company.bank(groupId):consumer-banking(artifactId):1.0(version)

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

All POMs inherit from a parent (despite explicitly defined or not).

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.

It helps developer to specify minimum configuration details in his/her pom.xml.

Although configurations can be overridden easily.

An easy way to look at the default configurations of the super POM is by running the following
command:

mvn help:effective-pom Create a pom.xml in any directory on your computer.

Use the content of above mentioned example pom.

In example below, We've created a pom.xml in C:\MVN\project folder.

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.

Maven repositories are of three types

 Local: a folder location on your machine. default - %USER_HOME% directory


It gets created(at default) when you run any maven command for the first time.
When you run a Maven build, then Maven automatically downloads all the dependency
jars into the local repository.
It helps to avoid references to dependencies stored on remote machine every time a
project is build.
To override the default location, mention another path in Maven settings.xml file
available at %M2_HOME%\conf directory.
 Central: Maven central repository is repository provided by Maven community.
It contains a large number of commonly used libraries.
When Maven does not find any dependency in local repository, it starts searching in
central repository using following URL:
 http://repo1.maven.org/maven2/
It requires internet access to be searched.
 Remote: Remote Repository is developer's own custom repository.
This contains required libraries or other project jars which are not available in central
repository

USER_HOME
 Unix/Mac OS X – ~/.m2
 Windows – C:\Documents and Settings\{your-username}\.m2
o C:\Users\Samrat\.m2\repository

M2_HOME – Maven home directory.


 C:\Manish\Appl_Setups\apache-maven-3.2.5

<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>

Maven Dependency Search Sequence:


 Search dependency in local repository, if not found, move to step 2 else if found then do the
further processing.
 Search dependency in central repository, if not found and remote repository/repositories is/are
mentioned then move to step 4 else if found, then it is downloaded to local repository for future
reference.
 If a remote repository has not been mentioned, Maven simply stops the processing and throws
error (Unable to find dependency).
 Search dependency in remote repository or repositories, if found then it is downloaded to local
repository for future reference otherwise Maven as expected stop processing and throws error
(Unable to find dependency).

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.

Example lib folder inside src folder.


If your code is using this library with Maven then Maven build will fail because it cannot download or
refer to this library during compilation phase.

To handle the situation, add this external dependency to maven pom.xml as

<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>

Key concepts about External Dependency.

External dependencies (library jar location) can be configured in pom.xml in same way as other
dependencies.

 Specify groupId same as name of the library.


 Specify artifactId same as name of the library.
 Specify scope as system.
 Specify system path relative to project location
Maven Plugins
Maven is actually a plugin execution framework where every task is done by plugins. Maven Plugins are
generally used to :

 create jar file


 create war file
 compile code files
 unit testing of code
 create project documentation
 create project reports

A plugin generally provides a set of goals and which can be executed using following syntax:

mvn [plugin-name]:[goal-name]

mvncompiler:compile

Java project can be compiled with the maven-compiler-plugin's compile-goal.

Plugin Types: Maven provided following two types of Plugins:


 Build plugins - They execute during the build and should be configured in the <build/> element
of pom.xml
 Reporting plugins - They execute during the site generation and they should be configured in
the <reporting/> element of the pom.xml

List of few common plugins:

 cleanClean up target after the build. Deletes the target directory.


 compiler Compiles Java source files.
 surefile Run the JUnit unit tests. Creates test reports.
 jar Builds a JAR file from the current project.
 war Builds a WAR file from the current project.
 javadoc Generates Javadoc for the project.
 antrun Runs a set of ant tasks from any phase mentioned of the build.
Let us understand maven-antrun-plugin which is used to print data on console.

<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

You might also like