This document discusses Apache Maven, an open-source tool for managing software projects and automating common software project tasks such as compiling source code, running tests, packaging, and deploying artifacts. It describes how Maven uses a Project Object Model (POM) file to manage a project's build, reporting and documentation from a central configuration. It also outlines key Maven concepts like the POM, lifecycles, phases, dependencies, repositories, inheritance, and multi-module projects.
2. So its done...
Im Renato Primavera from Quadratic
I write software that helps customers to manage and
make use of their geographical data
@RenatoPrimavera
renato.primavera@quadratic.be
www.quadratic.be
3. Maven can manage a project's
build, reporting and
documentation
from a central piece
of information: the POM
4. The concept of POM, for
Project Object Model
is the base of Maven
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
Here is a POM...
5. Maven makes the build process easy
> mvn clean
Deletes any build output (e.g. class files or JARs)
> mvn test
Runs the unit tests for the project
> mvn package
Build the project artifacts (e.g. JAR or WAR)
> mvn site
Creates project documentation (e.g. reports or Javadoc)
6. Maven provides a uniform build system
A project is always built using its
project object model (POM)
and a set of plugins that are shared by
all projects using Maven
8. Keeping your test source code in a separate, but
parallel source tree
Using test case naming conventions to locate and
execute tests
Layout your projects directory structure
(so that once you learn the layout, you can easily
navigate any other project that uses Maven and the
same defaults ! )
10. Maven Installation
Prerequisite: Maven is a Java tool, so you must have
Java installed in order to use it. More precisely, you need a
JDK, the JRE is not sufficient
First...
Download Maven (tar.gz / zip)
http://maven.apache.org/download.cgi
11. Then
Unzip the distribution archive to the
directory you wish to install Maven
Add the M2_HOME environment variable,
pointing to that directory
Append the value $M2_HOME/bin to the
PATH environment variable
#1
#2
#3
12. Make sure that JAVA_HOME exists as environment
variable and it is set to the location of your JDK
[Optional] add the MAVEN_OPTS environment
variable to specify JVM properties, e.g. the value
-Xms256m -Xmx512m. This can be used to supply
extra options to Maven
Open a command prompt and run mvn --version
to verify that it is correctly installed
#4
#5
#6
13. Maven is now installed. Its quite simple, isnt it?
Lets create a project
> mvn archetype:generate
-DgroupId=com.mycompany.app
-DartifactId=my-app
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
14. What has been generated?
(the project structure)
The POM file
The sources tree
The tests sources tree
16. What did you just do?
You executed the Maven goal archetype:
generate, and passed in various parameters to
that goal
The prefix archetype is the plugin that
contains the goal
17. The generate goal of the archetype
plugin created a simple project based upon
an archetype
Suffice it to say for now that a plugin is a collection of goals
with a general common purpose (e.g. the release plugin
provides goals that allow to release projects - updating the
POM and tagging in the SCM)
18. We can now Build that project
(and so generate a JAR artifact)
> mvn package
...
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO]
------------------------------------------------------------------------
The command line will print out various actions, and end with the following:
19. Unlike the first command executed
(archetype:generate) you may notice the
second is simply a single word - package
Rather than a goal, this is a phase
20. A phase is a step in the build lifecycle
(which is an ordered sequence of phases)
When a phase is given, Maven will
execute every phase in the sequence
up to and including the one defined
21. For example, if we execute the compile phase,
the phases that actually get executed are:
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
22. These are the most common default
lifecycle phases executed
validate: validate the project is correct and
all necessary information is available
compile: compile the source code of the
project
23. test: test the compiled source code using a
suitable unit testing framework. These tests
should not require the code be packaged or
deployed
package: take the compiled code and package
it in its distributable format, such as a JAR
24. integration-test: process and deploy the
package if necessary into an environment
where integration tests can be run
verify: run any checks to verify the package is
valid and meets quality criteria
25. 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
26. There are two other really useful
Maven lifecycles phases
clean: cleans up artifacts created by prior
builds
site: generates site documentation for this
project
28. Sir, yes Sir
A repository in Maven is used
to hold build artifacts and
dependencies of varying types
29. There are strictly only two types of
repositories: local and remote
The local repository refers to a copy on your own
installation that is a cache of the remote
downloads, and also contains the temporary build
artifacts that you have not yet released
31. Remote repositories refer to any other
type of repository, accessed by a variety of
protocols such as file:// and http://
Such repository might be a truly remote
repository set up by a third party to provide their
artifacts for downloading
32. Other "remote" repositories may be
internal repositories set up on a file or
HTTP server within your company
These are used to share private artifacts
between development teams and for releases
33. mvn install: installs the package
into the local repository
(for use as a dependency in other
projects locally)
Reminder
mvn deploy: deploys the final package to the
remote repository ,i.e. the corporate one
(for sharing with other developers and projects)
34. Before deploying any artifact
to a remote repository, you must
configure that repo !
Using the distributionManagement
section in your POM
or using the repositories
section in your settings.xml file
35. <distributionManagement>
<!-- use the following if you're not using a snapshot version. -->
<repository>
<id>repo</id>
<name>Repository Name</name>
<url>scp://host/path/to/repo</url>
</repository>
<!-- use the following if you ARE using a snapshot version. -->
<snapshotRepository>
<id>repo</id>
<name>Repository Name</name>
<url>scp://host/path/to/repo</url>
</snapshotRepository>
</distributionManagement>
in thePOM
37. The POM contains all necessary
information about a project, as well as
configurations of plugins to be used
during the build process
It is, effectively, the declarative manifestation of
the who, what and where of your project
(while the build lifecycle is the when and how)
38. The Super POM is
Maven's default POM
All POMs extend the Super POM unless explicitly
set, meaning the configuration specified in the
Super POM is inherited by the POMs you created
for your projects
39. By default, the build directory is target
By default, the source directory is src/main/java
By default, the test source directory is src/main/test
and so on...
Examples for this
40. Super POM brings also the
basic plugins...
maven-compiler-plugin
maven-dependency-plugin
maven-deploy-plugin
maven-jar-plugin
maven-javadoc-plugin
...
41. One powerful aspect of Maven
is in its handling of
project relationships
That includes dependencies
(and transitive dependencies),
inheritance, and aggregation (multi-
module projects)
43. Maven downloads and links
the dependencies for you
on compilation and other
goals that require them
44. As an added bonus, Maven brings in the
dependencies of those dependencies
(transitive dependencies), allowing your list
to focus solely on the dependencies your
project requires
45. One powerful addition that Maven
brings to build management is
the concept of
project
inheritance
48. Once defined, we may add
values to this parent POM,
which will be inherited by
its children
49. For instance, sections like
dependencyManagement (or
pluginManagement) can be defined
in the parent POM
Dependency management allows to directly specify the
versions of artifacts to be used when they are
encountered in transitive dependencies or in dependencies
of children POM and where no version has been specified
50. In another words, the dependency
management section is a mechanism
for centralizing dependency
information
When you have a set of projects that inherits a common
parent it's possible to put all information about the
dependency in the common POM and have simpler
references to the artifacts in the child POMs