2. Introduction
Maven is a software project management and automation tool
used primarily for Java projects.
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 uses an XML file to describe the software project being
built,
its
dependencies
on
other
external
modules
and
components, the build order, directories, and required plug-ins.
3. Convention and
configuration
When a Maven project is created, Maven creates default project
structure.
Developer is only required to place files accordingly and he/she
need not to define any configuration in pom.xml.
4. As an example, following table shows the default values for project
source code files, resource files and other configurations.
Assuming, ${basedir} denotes the project location:
source code
: ${basedir}/src/main/java
Resources
: ${basedir}/src/main/resources
Tests
: ${basedir}/src/test
Complied
byte code
: ${basedir}/target/classes
distributable JAR : ${basedir}/target
5. Maven POM
• POM stands for Project Object Model.
• It is fundamental Unit of Work in Maven. It is an XML file.
• A file with this name has to exist in the root of every maven
project.
• In this file are defined the plugins to use,paths and settings to
override the maven defaults of your project.
• Each POM inherits automatically from a superPOM,a virtual
POM embedded in the Maven core.
6. Some of the configuration that can be specified in the POM are
following:
project dependencies
plugins
goals
build profiles
project version
developers
mailing list
7. The pom describes the project and its dependencies.Each with
3 "coordinates":
-Group ID
:This is an Id of project's group. This is
generally unique amongst an organization or a project. For
example, a banking group com.company.bank has all bank
related projects.
-Artifact ID (common) :This is generally name of the project.
-Version (1.0)
:This is the version of the project.
8.
Maven is declarative.
Specify the jars used,where they come from,and what
release,and maven will manage the files without
explicit
coding.
Even if the jar filename has no version,Maven keeps track.
While executing a task or goal, Maven looks for the POM in the
current directory. It reads the POM, gets the needed configuration
information, then executes the goal.
9. Maven Repositories
A repository is a place i.e. 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 repository are of three types
• local
• central
• remote
10. Local Repository
• It gets created when you run any maven command for the first
time.
• In simple, when you build a Maven project, all dependency files
will be stored in your Maven local repository.
•
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.
11. •
Maven local repository by default get created by Maven in
%USER_HOME% directory. To override the default location,
mention another path in Maven settings.xml file available at
%M2_HOME%conf directory.
13. Central Repository
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/
Key concepts of Central repository
•
This repository is managed by Maven community.
•
It is not required to be configured.
•
It requires internet access to be searched.
14. Remote Repository
It is developer's own custom repository containing required libraries or other
project jars.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven4.0.0.xsd"> <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>
15. Example for Dependency
Consider a team is developing a project bus-core-api on which two other
projects app-web-ui and app-desktop-ui are dependent.
bus-core-api project
<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>bus-core-api</groupId>
<artifactId>bus-core-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
16. app-web-ui project is using 1.0-SNAPSHOT of bus-core-api project
<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>app-web-ui</groupId>
<artifactId>app-web-ui</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>bus-core-api</groupId>
<artifactId>bus-core-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
17. app-desktop-ui project is using 1.0-SNAPSHOT of bus-core-api
project
<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>app-desktop-ui</groupId>
<artifactId>app-desktop-ui</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>bus-core-api</groupId>
<artifactId>bus-core-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
18. Snapshot vs Version
In case of Version, if Maven once downloaded the mentioned
version say data-service:1.0, it will never try to download a newer
1.0 available in repository. To download the updated code, dataservice version is be upgraded to 1.1.
In case of SNAPSHOT, Maven will automatically fetch the latest
SNAPSHOT (data-service:1.0-SNAPSHOT) everytime app-ui team
build their project.
19. Ant Vs Maven
A Simple Ant build.xml file.
<project name="my-project" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src/main/java"/>
<property name="build" location="target/classes"/>
<property name="dist" location="target"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
20. A Sample Maven pom.xml.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
</project>
You have to tell Ant exactly where your source is, where you
want the resulting bytecode to be stored, and how to package
this all into a JAR file.
21.
Running mvn install from the command line will process
resources, compile source, execute unit tests, create a JAR, and
install the JAR in a local repository for reuse in other projects.
In Maven, to create a JAR file from some Java source, all you
need to do is
create a simple pom.xml, place your source code in $
{basedir}/src/main/java and then run mvn install from the
command line.
22. Advantages of Using Maven
Making the build process easy and provide uniform
build environment.
Reusable plugins, repositories.
Dependencies are downloaded automatically.
Standardized, very consistent naming.
Maven forces you to have a standard directory
structure