Maven nutshell
-
Upload
valerio-capozio -
Category
Technology
-
view
499 -
download
3
description
Transcript of Maven nutshell
1 of 14 Valerio Capozio
Maven Nutshell The Main Concepts
2 of 14 Valerio Capozio
Apache Maven
Maven is a build automation tool, developed and managed from Apache Software
Foundation.
Maven serves a similar purpose of Apache Ant tool, but it offers an unparalleled software
lifecycle management, providing a cohesive suite of verification, compilation, testing, packaging,
reporting, and deployment plugins.
Maven uses a convention over configuration approach to builds.
Maven is typically used in Java projects, but it can be used also to manage projects in C#,
Scala, Ruby and more other languages.
3 of 14 Valerio Capozio
Project Object Model
Maven revolves around a metadata file, named
pom.xml
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 Super
POM, a virtual POM embedded in the Maven core.
Maven supports natively multi-module projects.
The snippet above shows the minimum information needed to define uniquely a Maven project through a Pom.
A Maven project produces an element, such as JAR, WAR, EAR, etc uniquely identified by a vector of fields (groupId, artifactId, packaging, version).
A syntax to refer a specific Maven artifact is a string of vector elements, colon separated:
groupId:artifactId:packaging:version
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
4 of 14 Valerio Capozio
Execution Hierarchy
Maven splits execution into four nested hierarchies.
From most-generic to most-specific they are:
Lifecycle
Phase
Plugin
Goal
Lifecycle represents a well-recognized flow of steps.
Phase is a step of lifecycle. Zero or more plugin goals
are bound to a phase.
Plugin is a logical grouping and distribution of related
goals.
Goal is a single executable task within a plugin.
5 of 14 Valerio Capozio
Built-in Maven Lifecycle
Maven has three built-in lifecycles:
Clean
Default
Site
Many of the phases within these three lifecycles are bound to a sensible plugin goal.
Clean lifecycle deletes all generated artifacts from the output directory.
Default lifecycle defines the commonly used phases to build an artifact, from its compilation
to its installation on remote repository.
Site lifecycle generates a project information web site and is able to deploy the artifacts to a
specified web server or local path.
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
6 of 14 Valerio Capozio
Phases of Clean and Site Lifecycles
Clean Lifecycle
Phase Purpose
pre-clean -
clean Remove all generated and compiled artifacts in preperation for a fresh build.
post-clean -
Site Lifecycle
Phase Purpose
pre-site Cross check that all elements necessary for the build are correct and present.
site Generate an HTML web site containing project information and reports.
post-site -
site-deploy Upload the generated website to a web server.
7 of 14 Valerio Capozio
Phases of Default Lifecycle 1/2
Default Lifecycle
Phase Purpose validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation (e.g. to do bytecode enhancement on classes)
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation (e.g. to do bytecode enhancement on classes)
8 of 14 Valerio Capozio
Phases of Default Lifecycle 2/2
Default Lifecycle
Phase Purpose test run tests using a suitable unit testing framework. These tests should not require the code be
packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
9 of 14 Valerio Capozio
Dependency
To express the reliance of our project on
a particular artifact is possible declare a
dependency in the Pom file.
Each dependency can specify a scope,
which manages its visibility and inclusion
in the final artifact.
Maven manages automatically the
transitive dependencies.
Dependency scope can affect the
transitivity mechanism.
Scope Description
compile Needed for compilation, included in packages.
test Needed for unit tests, not included in packages.
provided Needed for compilation, but provided ar runtime.
system Needed for compilation, given as absolute path on disk and not included in packages.
import Inline inclusion of a POM-type artifact.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
10 of 14 Valerio Capozio
Plugins
A plugin and its configuration are added
with a declaration, very similar to a
dependency, in the build section of the
Pom file.
Mojo is the acronym of the Maven
plugin classes and derives from
aggregation of “Plain Old Java Object” and
“Maven Java Object”.
Common Plugins
surefire Runs unit tests
checkstile Check the code’s styling
assembly Create ZIP and other distribution packages.
11 of 14 Valerio Capozio
Repository
Repositories are used to hold collections
of Maven artifacts and dependencies.
Exist only two type of repositories: local
and remote.
Local repository refers to a copy on your
own installation. This repository is a cache
copy of the remote downloads.
Remote repositories are any other type
of repository accessed by some protocol
(e.g. http:// file:// etc.)
Popular Repositories
Maven http://repo1.maven.org/maven2/
Codehaus http://repository.codehaus.org/
JBoss http://repository.jboss.org/maven2
12 of 14 Valerio Capozio
Profiles
With profiles can specify particular
behaviors on portions of Maven
configuration, including plugins, pathing and
configuration.
They modify the POM at build time, and
are meant to be used in complementary sets
to give equivalent-but-different parameters
for a set of target environments.
A typical use of profiles is for build-time
customization of JAR dependencies based on
the use of a specific Application Server.
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
The snippet above shows an example of profile declaration.
A profile can be triggered/activated in several ways:
Explicitly
Through Maven settings
Based on environment variables
OS settings
Present or missing files
13 of 14 Valerio Capozio
About me
Website
angelusworld.com
linkendin.com
Slideshare
slideshare.net
twitter.com
Valerio Capozio
14 of 14 Valerio Capozio
Thanks