4. Definition
● Apache Maven is a software project management
and comprehension tool.
● Based on the concept of a project object model
(POM), Maven can manage a project's build,
binaries, reporting and documentation from a
central piece of information.
● Apache Maven is a command line tool with some
IDE integrations.
Licensed under a Creative Commons license
8. Dependencies
● Declaratives
- groupId + artifactId + version (+ classifier)
- Type (packaging) : jar, war, pom, ear, …
● Transitives
- Lib A needs Lib B
- Lib B needs Lib C
- Thus Lib A needs Lib C
Licensed under a Creative Commons license
9. Dependencies
● Scope
- Compile (by default) : Required to build and run the
application
- Runtime : not required to build the application but
needed at runtime
● Ex : taglibs
- Provided : required to build the application but not
needed at runtime (provided by the container)
● Ex : Servlet API, Driver SGBD, …
- Test : required to build and launch tests but not needed
by the application itself to build and run
● Ex : Junit, TestNG, DbUnit, …
- System : local library with absolute path
● Ex : software products
Licensed under a Creative Commons license
10. Dependencies
● Define all dependencies you are using
- and no more !
● Cleanup your dependencies with
- mvn dependency:analyze!
● Study your dependencies with
- mvn dependency:tree!
- mvn dependency:list!
Licensed under a Creative Commons license
11. Artifact Repository
● By default :
- A central repository
● http://repo1.maven.org/
maven2
● Several dozen of Gb of OSS
libraries
- A local repository
● ${user.home}/.m2/repository
● All artifacts
- Used by maven and its
plugins
- Used by your projects
(dependencies)
- Produced by your projects
Licensed under a Creative Commons license
12. Artifact Repository
● By default Maven
downloads artifacts
required by the project or
itself from central
● Downloaded artifacts are
stored in the local
repository
Licensed under a Creative Commons license
13. Versions
● Project and dependency versions
● Two different version variants
SNAPSHOT version
● The version number ends with –SNAPSHOT
● The project is in development
● Deliveries are changing over the time and are overridden
after each build
● Artifacts are deployed with a timestamp on remote
repositories
RELEASE version
● The version number doesn’t end with –SNAPSHOT
● Binaries won’t change
Licensed under a Creative Commons license
15. Versions
● About SNAPSHOT dependencies
- Maven allows the configuration of an update policy.
The update policy defines the recurrence of checks if
there is a new SNAPSHOT version available on the
remote repository :
● always
● daily (by default)
● interval:X (a given period in minutes)
● never
- Must not be used in a released project
● They can change thus the release also
● The release plugin will enforce it
Licensed under a Creative Commons license
16. Versions
● Use the versions plugin to update all versions in all
POMs
- mvn versions:set
–DnewVersion=A.B.C-SNAPSHOT!
Licensed under a Creative Commons license
17. Profiles
● Allow to modify the default behavior of Maven by
overriding/adding some settings
● Use mvn help:active-profiles to debug
● Explicit activation or deactivation
mvn <phases or goals> !
-PprofileId1,-profileId2 !
-P!profileId3!
Licensed under a Creative Commons license
18. Profiles
● activeByDefault = If no other profile is activated
● Activation through Maven settings
<settings>!
...!
<activeProfiles>!
<activeProfile>profile-1</activeProfile>!
</activeProfiles>!
...!
</settings>!
Licensed under a Creative Commons license
19. Profiles
● Activation based on environment variables
<profiles>! <profiles>!
<profile>! <profile>!
<activation>! <activation>!
<property>! <property>!
<name>!skip-enforce</name>!
<name>run-its</name>!
</property>!
<value>true</value>!
</activation>!
</property>!
...!
</activation>!
</profile>!
...!
</profiles>!
</profile>!
</profiles>!
Licensed under a Creative Commons license
21. Profiles
● Activation on present or missing files
<profiles>!
<profile>!
<activation>!
<file>!
<missing>${project.build.directory}/generated-sources/
axistools/wsdl2java/org/apache/maven</missing>!
</file>!
</activation>!
...!
</profile>!
</profiles>!
Licensed under a Creative Commons license
22. Reactor
● Ability of Maven to build pom.xml :
several sub-modules <modules>
resolving the order of their <module>moduleA</module>
dependencies <module>moduleC</module>
● Modules have to be <module>moduleB</module>
defined in the POM </modules>
- For a performance reasons
Licensed under a Creative Commons license
23. Inheritence
● Share settings between
projects/modules
● Project
- Business1
● Jar
● War
- Business2
● Jar pom.xml for module Jar1
● War <parent>
● By default the parent <groupId>X.Y.Z</groupId>
project is supposed to be <artifactId>jars</artifactId>
in the parent directory <version>1.0-SNAPSHOT<version>
(../) </parent>
Licensed under a Creative Commons license
24. Build Lifecycle And Plugins
● Plugin based architecture
for a great extensibility
● Standardized lifecycle to
build all types of
archetypes
Licensed under a Creative Commons license
27. K.I.S.S.
● Keep It Simple, Stupid
● Start from scratch
- Do not copy/paste what you find without understanding
● Use only what you need
- It’s not because maven offers many features that you
need to use them
● Filtering
● Modules
● Profiles
● …
Licensed under a Creative Commons license
29. Project bad practices
● Ignore Maven conventions
- Except if your are migrating from something else and
the target has to be to follow them.
- Except if they are not compatible with your IDE
● Different versions in sub-modules
- In that case they are standalone projects.
● Too many inheritance levels
- It makes the POMs maintenance more complex
- Where should I set this plugin parameter ? In which parent ?
Licensed under a Creative Commons license
30. Project bad practices
● Have too many modules
- Is there a good reason ?
● Technical constraint ?
● Team organization ?
- It increases the build time
● Many more artifacts to generate
● Dependencies resolution more complex
- It involves more complex developments
● More modules to import in your IDE
● More modules to update …
Licensed under a Creative Commons license
31. Project good practices
● Use the default
inheritance :
- The reactor project is also
the parent of its modules.
- Configuration is easier :
● No need to redefine SCM
settings, site distribution
settings …
Licensed under a Creative Commons license
33. POM bad practices
● Dependencies :
- DON’T confuse dependencies and
dependencyManagement
● Plugins :
- DON’T confuse plugins and pluginManagement
- DON’T use AntRun plugin everywhere
- DON’T let Maven choose plugins versions for you
Licensed under a Creative Commons license
34. POM bad practices
● Profiles :
- DON’T create environment dependant builds
- DON’T rely on dependencies coming from profiles
(there is no transitive activation of profiles)
● Reporting and quality
- DON’T activate on an existing project all reports with
default configuration
- DON’T control formatting rules without giving settings
for IDEs.
● DON’T put everything you find in your POM.
Licensed under a Creative Commons license
35. POM good practices
● Set versions of dependencies in project parent’s
dependencyManagement
● Set dependencies (groupId, artifactId, scope) in
each module they are used
● Use the dependency plugin (from apache) and
versions plugin (from mojo) to analyze, cleanup
and update your dependencies.
Licensed under a Creative Commons license
37. Development bad practices
● DON’T spend your time in the terminal,
● DON’T exchange libraries through emails,
● DON’T always use "-Dmaven.test.skip=true”
● DON’T manually do releases
Licensed under a Creative Commons license
38. Development good practices
● Keep up-to-date your version of Maven
- For example in 2.1 the time of dependencies/modules
resolution decreased a lot (Initialization of a project of
150 modules passed from 8 minutes to less than 1)
● Use the reactor plugin (Maven < 2.1) or native
reactor command line options (Maven >= 2.1) to
rebuild only a subpart of your project :
- All modules depending on module XXX
- All modules used to build XXX
● Try to not use Maven features not supported by
your IDE (resources filtering with the plugin
eclipse:eclipse)
Licensed under a Creative Commons license
41. Some links
● The main web site :
- http://maven.apache.org
● Project’s team wiki :
- http://docs.codehaus.org/display/MAVEN
● Project’s users wiki :
- http://docs.codehaus.org/display/MAVENUSER
Licensed under a Creative Commons license
42. Books
● Sonatype / O’Reilly :
- The Definitive Guide
- http://www.sonatype.com/
books
- Free download
- Available in several
languages
● Soon in French
Licensed under a Creative Commons license
43. Books
● Exist Global
- Better builds with Maven
- http://www.maestrodev.com/
better-build-maven
- Free download
Licensed under a Creative Commons license
44. Books
● Nicolas De loof
Arnaud Héritier
- Published by Pearson
- Collection Référence
- Based on our own
experiences with Maven.
From beginners to experts.
- In French only
- Available on 20th November
Licensed under a Creative Commons license
46. Licence et copyrights
● Photos and logos belong to their respective
authors/owners
● Content under Creative Commons 3.0
- Attribution — You must attribute the work in the manner specified by the
author or licensor (but not in any way that suggests that they endorse you or
your use of the work).
- Noncommercial — You may not use this work for commercial
purposes.
- Share Alike — If you alter, transform, or build upon this work, you may
distribute the resulting work only under the same or similar license to this one.
● http://creativecommons.org/licenses/by-nc-sa/3.0/us/
Licensed under a Creative Commons license