141
Apache Maven – LorraineJUG June 1st, 2010 Arnaud Héritier eXo Platform Software Factory Manager

Lorraine JUG (1st June, 2010) - Maven

Embed Size (px)

DESCRIPTION

Overview of Maven and its concepts Maven, and the future of Maven 3.x Maven and its ecosystem Good and bad practices Usecases

Citation preview

Page 1: Lorraine JUG (1st June, 2010) - Maven

Apache Maven – LorraineJUG June 1st, 2010

Arnaud Héritier eXo Platform

Software Factory Manager

Page 2: Lorraine JUG (1st June, 2010) - Maven

2

Arnaud Héritier •  Software Factory Manager

eXo platform »  In charge of tools and methods

•  Committer since 2004 and member of the Project Management Committee

•  Coauthor of « Apache Maven » published by Pearson (in French)

•  Contact me : » http://aheritier.net » Twitter : @aheritier » Skype : aheritier

Page 3: Lorraine JUG (1st June, 2010) - Maven

CHOOSE YOUR MENU Apache Maven

Page 4: Lorraine JUG (1st June, 2010) - Maven

4

Overview

•  Definition •  History •  Concepts

» Conventions » POM » Reactor and Modules »  Inheritance » Artifact Repository » Dependency » Version » Profiles » Build Lifecycle And Plugins

•  Maven or not Maven, that is the question ! » Maven, the project

choice » Maven, the corporate

choice » Competitors

Page 5: Lorraine JUG (1st June, 2010) - Maven

5

Back to the future

•  Maven 2.x •  Maven 3.x •  Community

Page 6: Lorraine JUG (1st June, 2010) - Maven

6

Ecosystem

•  Repository Managers •  Quality Management

» Tests Automation » Quality Metrics Reports » Project Reports » Sonar

•  Continuous Integration •  IDE

» Eclipse » Idea IntelliJ » Netbeans

Page 7: Lorraine JUG (1st June, 2010) - Maven

7

Good & Bad Practices

•  K.I.S.S. •  Project Organization •  POM •  Development

Page 8: Lorraine JUG (1st June, 2010) - Maven

8

Usecases

•  Secure your credentials •  Build a part of your project using reactor

options •  Automate your release process

» (at least the technical part) •  Setup a global mirror

Page 9: Lorraine JUG (1st June, 2010) - Maven

OVERVIEW Apache Maven

Page 10: Lorraine JUG (1st June, 2010) - Maven

10

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.

Page 11: Lorraine JUG (1st June, 2010) - Maven

11

History

•  Initiated in 2001 by Jason Van Zyl in Alexandria, an Apache Jakarta project,

•  Moved to Turbine few months after, •  Became a Top Level Project in 2003. •  Maven 2.0 released in September 2005 •  Maven 3.0 … coming soon !!!

Page 12: Lorraine JUG (1st June, 2010) - Maven

12

CONCEPTS Apache Maven

Page 13: Lorraine JUG (1st June, 2010) - Maven

13

Conventions

•  1 project = 1 artifact (pom, jar, war, ear, …) •  Standardized

» project descriptor (POM) » build lifecycle » directories layout

•  *.java to compile in src/[main|test]/java

•  *.xml, *.properties needed in classpath and to bundle in archive in src/[main|test]/resources

•  target directory for generated stuffs (sources, classes, …) •  …

Page 14: Lorraine JUG (1st June, 2010) - Maven

14

POM

•  An XML file (pom.xml)

•  Describing » Project identification » Project version » Project description » Build settings » Dependencies » …

<?xml version="1.0" encoding="UTF-8"?>!

<project>!

<modelVersion>4.0.0</modelVersion>! <groupId>net.aheritier.samples</groupId>!

<artifactId>simple-webapp</artifactId>!

<version>1.1-SNAPSHOT</version>!

<packaging>war</packaging>! <name>Simple webapp</name>!

<inceptionYear>2007</inceptionYear>!

<dependencies>!

<dependency>! <groupId>org.springframework</groupId>!

<artifactId>spring-struts</artifactId>!

<version>2.0.2</version>!

</dependency>! ...!

</dependencies>!

</project>!

Page 15: Lorraine JUG (1st June, 2010) - Maven

15

Reactor

<project>!

...!

<modules>!

<module>moduleA</module>!

<module>moduleB</module>!

<module>moduleC</module>!

<module>moduleD</module>!

<module>moduleE</module> !

<module>moduleF</module>!

</modules>!

...!

</project>!

•  Split your project in sub-modules

•  Maven computes the build order from dependencies between sub-modules.

•  Modules have to be defined in the POM » No auto-discovery for

performance reasons

Page 16: Lorraine JUG (1st June, 2010) - Maven

16

Inheritance

•  Share settings between projects/modules

•  By default the parent project is supposed to be in the parent directory (../)

<parent>!

<groupId>net.aheritier.sample</groupId>!

<artifactId>my-parent</artifactId>!

<version>1.0.0-SNAPSHOT<version>!

</parent>!

Page 17: Lorraine JUG (1st June, 2010) - Maven

17

Inheritance

Insert README in all artifacts

Use assembly to package batchs

Use clirr to validate backward compatibility

Use a technical inheritance to organize sub-modules

Page 18: Lorraine JUG (1st June, 2010) - Maven

18

Artifact Repository

Page 19: Lorraine JUG (1st June, 2010) - Maven

19

Artifact Repository

•  By default Maven downloads artifacts required by the project or itself from central

•  Downloaded artifacts are stored in the local repository

•  Used to store : » Project’s binaries » Project’s dependencies » Maven and plug-ins

binaries

Page 20: Lorraine JUG (1st June, 2010) - Maven

20

Dependencies

Without Maven With Maven

Page 21: Lorraine JUG (1st June, 2010) - Maven

21

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

Page 22: Lorraine JUG (1st June, 2010) - Maven

22

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

Page 23: Lorraine JUG (1st June, 2010) - Maven

23

Dependencies

•  Define all dependencies you are using » and no more !

•  If you have optional dependencies » Perhaps you should have optional modules instead

•  Cleanup your dependencies with » mvn dependency:analyze!

•  Study your dependencies with » mvn dependency:tree!» mvn dependency:list!

Page 24: Lorraine JUG (1st June, 2010) - Maven

24

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

Page 25: Lorraine JUG (1st June, 2010) - Maven

25

Versions

Page 26: Lorraine JUG (1st June, 2010) - Maven

26

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

Page 27: Lorraine JUG (1st June, 2010) - Maven

27

Versions

•  Range » From … to … » Maven automatically searches for the

corresponding version (using the update policy for released artifacts)

» To use with caution •  Risk of non reproducibility of the build •  Risk of side effects on projects depending on yours.

Page 28: Lorraine JUG (1st June, 2010) - Maven

28

Versions

•  Use the versions plugin to update all versions of your project and its modules

mvn versions:set –DnewVersion=A.B.C-SNAPSHOT!

Page 29: Lorraine JUG (1st June, 2010) - Maven

29

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!

Page 30: Lorraine JUG (1st June, 2010) - Maven

30

Profiles

●  activeByDefault = If no other profile is activated

●  Activation through Maven settings

<settings>!

...!

<activeProfiles>!

<activeProfile>profile-1</activeProfile>!

</activeProfiles>!

...!

</settings>!

Page 31: Lorraine JUG (1st June, 2010) - Maven

31

Profiles

•  Activation based on environment variables

<profiles>!

<profile>!

<activation>!

<property>!

<name>!skip-enforce</name>!

</property>!

</activation>!

...!

</profile>!

</profiles>!

<profiles>!

<profile>!

<activation>!

<property>!

<name>run-its</name>!

<value>true</value>!

</property>!

</activation>!

...!

</profile>!

</profiles>!

Page 32: Lorraine JUG (1st June, 2010) - Maven

32

Profiles

•  OS / Java settings <profiles>!

<profile>!

<activation>!

<os>!

<name>Windows XP</name>!

<family>Windows</family>!

<arch>x86</arch>!

<version>5.1.2600</version>!

</os>!

</activation>!

...!

</profile>!

</profiles>!

<profiles>!

<profile>!

<activation>!

<jdk>[1.3,1.6)</jdk>!

</activation>!

...!

</profile>!

</profiles>!

Page 33: Lorraine JUG (1st June, 2010) - Maven

33

Profiles

•  Activation on present or missing files <profiles>!

<profile>!

<activation>!

<file>!

<missing>${project.build.directory}/generated-sources/axistools/wsdl2java/</missing>!

</file>!

</activation>!

...!

</profile>!

</profiles>!

Page 34: Lorraine JUG (1st June, 2010) - Maven

34

Build Lifecycle And Plugins

•  Plugin based architecture for a great extensibility

•  Standardized lifecycle to build all types of archetypes

Page 35: Lorraine JUG (1st June, 2010) - Maven

35

Build Lifecycle And Plugins Default Lifecycle Clean Lifecycle Site Lifecycle validate pre-clean pre-site

initialize clean site generate-sources post-clean post-site

process-sources site-deploy generate-resources

process-resources

compile process-classes

generate-test-sources

process-test-sources

generate-test-resources

process-test-resources

test-compile

process-test-classes

test prepare-package

package pre-integration-test

integration-test

post-integration-test

verify

install deploy

Page 36: Lorraine JUG (1st June, 2010) - Maven

36

Build Lifecycle And Plugins

•  Many plugins » Packaging » Reporting » IDE integration » Miscellaneous tools integration

•  Many locations » maven.apache.org » mojo.codehaus.org » code.google.com » …

Take care while selecting them !!!

Page 37: Lorraine JUG (1st June, 2010) - Maven

37

MAVEN OR NOT MAVEN, THAT IS THE QUESTION ! Apache Maven

Page 38: Lorraine JUG (1st June, 2010) - Maven

38

Maven, the project’s choice

•  Application’s architecture » The project has the freedom to divide the application in

modules » Maven doesn’t limit the evolution of the application

architecture

•  Dependencies management » Declarative : Maven automatically downloads them and

builds the classpath » Transitive : We define only what the module needs itself

Page 39: Lorraine JUG (1st June, 2010) - Maven

39

Maven, the project’s choice

•  Centralizes and automates all development facets (build, tests, releases)

•  One thing it cannot do for you : to develop

» Builds » Tests » Packages » Deploys » Documents » Checks and reports

about the quality of developments

Page 40: Lorraine JUG (1st June, 2010) - Maven

40

Maven, the corporate’s choice

•  Widely adopted and known » Many developers

•  Developments are standardized •  Decrease of costs

» Reuse of knowledge » Reuse of configuration fragments » Reuse of process and code fragments

•  Product quality improvement » Reports and monitoring

Page 41: Lorraine JUG (1st June, 2010) - Maven

41

Competitors

•  Ant + Ivy, Easy Ant, Gant, Gradle, Buildr… •  Script oriented

» You can do what you want ! •  Reuse many of Maven conventions (directories layout,

…) and services (repositories) but without enforcing them

•  The risk for them : Not being able to evolve due to the too high level of customization proposed to the user. » We tried on Maven 1 and it died because of that. It was

impossible to create a set of tests to cover all usages. »  It’s like providing a framework without public API

Page 42: Lorraine JUG (1st June, 2010) - Maven

42

With scripts oriented builds

You can have (if you have good skills)

But often you have (moreover after years …)

Page 43: Lorraine JUG (1st June, 2010) - Maven

43

With Maven

We dream to deliver (Maven 3.x)

But today we have too often (Maven 2.x)

Page 44: Lorraine JUG (1st June, 2010) - Maven

BACK TO THE FUTURE Apache Maven

Page 45: Lorraine JUG (1st June, 2010) - Maven

45

PRODUCT Apache Maven

Page 46: Lorraine JUG (1st June, 2010) - Maven

46

Apache Maven 2.0.x

•  bugs fix •  Last release : 2.0.11 •  No other release of 2.0.x in the future

Page 47: Lorraine JUG (1st June, 2010) - Maven

47

Apache Maven 2.x

•  Evolutions, new features •  Several important new features in 2.1 like

» Parallel downloads » Encrypted passwords » Reactor command line options

•  Last release : 2.2.1

Page 48: Lorraine JUG (1st June, 2010) - Maven

48

Apache Maven 3.x

•  Do not be afraid !!!!! •  Full compatibility with maven 2.x projects

» Or at least at 99,99999% •  Availability in 2010 (2nd half)

Page 49: Lorraine JUG (1st June, 2010) - Maven

49

Apache Maven 3.x – Why ?

» To build new foundations for the future » The major part of the code was reviewed / rewritten

•  How POMs are constructed •  How the lifecycle is executed •  How the plugin manager executes •  How artifacts are resolved •  How it can be embedded •  How dependency injection is done •  …

Page 50: Lorraine JUG (1st June, 2010) - Maven

50

Apache Maven 3.x - robustness

•  Error & integrity reporting » Much improved error reporting where we will provide

links to each identifiable problem we know of. There are currently 42 common things that can go wrong.

» Don't allow builds where versions come from non-project sources like local settings and CLI parameters

» Don't allow builds where versions come from profiles that have to be activated manually

•  Backward compatibility » Several thousands of integration tests

Page 51: Lorraine JUG (1st June, 2010) - Maven

51

Apache Maven 3.x - performances

•  Many optimizations •  New support of parallel builds of modules •  New incremental (partial) build

» To improve IDE integration

Page 52: Lorraine JUG (1st June, 2010) - Maven

52

Apache Maven 3.x – new features

•  Any-source POM » If you don’t like XML, choose another DSL

•  Versionless parent elements » If you don’t use versions or release plugins to

automatically update them •  Mixins

» a compositional form of Maven POM configuration •  Global excludes

Page 53: Lorraine JUG (1st June, 2010) - Maven

53

Apache Maven 3.x

•  What it will change for maven developers ? » Lifecycle extension points » Plugin extension points » Incremental build support » Queryable lifecycle » Extensible reporting » Bye bye Plexus, welcome JSR 330 & Google Guice » Well defined and documented APIs

Page 54: Lorraine JUG (1st June, 2010) - Maven

54

Apache Maven 3.x – New tools

•  mvnsh » A cross-platform shell

dedicated to maven •  Tycho

» Maven ready for OSGI and Eclipse developments

Page 55: Lorraine JUG (1st June, 2010) - Maven

55

In Apache Maven 3.0 ?

•  A backward compatibility near to 100% for projects and plugins

•  A totally new implementation » A greater robustness with a better reporting and

more readable logs » Performances improvements and new parallel

builds » A better integration for others tools like IDE or

continuous integration servers •  No change in current POM format

Page 56: Lorraine JUG (1st June, 2010) - Maven

56

COMMUNITY Apache Maven

Page 57: Lorraine JUG (1st June, 2010) - Maven

57

Users Mailing List

•  Blue : » Number of subscribers

•  Red : » Number of messages

per day

•  [email protected] » Traffic statistics cover a

total of 1697 days. » Current subscribers: 1861 » Current digest

subscribers: 47 » Total posts (1697 days):

80633 » Mean posts per day: 47.52

•  http://pulse.apache.org/

Page 58: Lorraine JUG (1st June, 2010) - Maven

58

Apache Maven Web Site

Page 59: Lorraine JUG (1st June, 2010) - Maven

59

Dowloads

Per month downloads http://people.apache.org/~vgritsenko/stats/

projects/maven.html

Page 60: Lorraine JUG (1st June, 2010) - Maven

60

The team

•  60 committers, •  More than 30 active in 2009, •  Several organizations like Sonatype, deliver

resources and professional support, •  A community less isolated : more interactions

with Eclipse, Jetty,

Page 61: Lorraine JUG (1st June, 2010) - Maven

61

Commit Statistics

Page 62: Lorraine JUG (1st June, 2010) - Maven

ECOSYSTEM Apache Maven

Page 63: Lorraine JUG (1st June, 2010) - Maven

63

Maven’s ecosystem

•  Maven alone is nothing •  You can integrate it with many tools

» A large set of plug-ins is already available » You can define your own plug-ins

Page 64: Lorraine JUG (1st June, 2010) - Maven

64

REPOSITORY MANAGERS Apache Maven

Page 65: Lorraine JUG (1st June, 2010) - Maven

65

Repository Managers

•  Basic services » Search artifacts » Browse repositories » Proxy external repositories » Host internal repositories » Security

•  Several products » Sonatype Nexus

(replaced Proximity) » Jfrog Artifactory » Apache Archiva

Page 66: Lorraine JUG (1st June, 2010) - Maven

66

Secure your builds

•  Deploy a repository manager to proxy externals repositories to : » Avoid external network outages » Avoid external repository unavailabilities » To reduce your company’s external network usage » To increase the speed of artifact downloads

•  Additional services offered by such servers : » Artifacts procurement to filter what is coming from the

outside » Staging repository to validate your release before

deploying it

Page 67: Lorraine JUG (1st June, 2010) - Maven

67

Nexus at eXo for productivity

Page 68: Lorraine JUG (1st June, 2010) - Maven

68

Nexus at eXo for collaboration

•  Deploy 3rd Party Artifacts

•  Collaborate with Internal Repositories

•  Distribute to the community with Public Repositories

•  Distribute to customers with Private Repositories

Page 69: Lorraine JUG (1st June, 2010) - Maven

69

Nexus at eXo for quality

•  Ease the Burden on Central and others remote repositories

•  Gain Predictability and Scalability •  Control and Audit Dependencies and Releases •  Stage releases

Page 70: Lorraine JUG (1st June, 2010) - Maven

70

QUALITY MANAGEMENT Apache Maven

Page 71: Lorraine JUG (1st June, 2010) - Maven

71

Tests Automation

•  Use automated tests as often as you can •  Many tools are available through Maven

» JUnit, TestNG – unit tests, » Selenium, Canoo – web GUI test, » Fitnesse, Greenpepper – functional tests, » SoapUI – web services tests » JMeter – performances tests » And many more frameworks are available to reply

your needs

Page 72: Lorraine JUG (1st June, 2010) - Maven

72

Quality Metrics

•  Extract quality metrics from your project and monitor them : » Code style (CheckStyle) » Bad practices or potential bugs (PMD, FindBugs, Clirr) » Tests coverage (Cobertura, Emma, Clover) » …

•  You can use blocking rules » For example, I break the build if the upward compatibility of

public APIs is broken •  You can use reports

» Reports are available in a web site generated by Maven » Or in a quality dashboard like Sonar

Page 73: Lorraine JUG (1st June, 2010) - Maven

73

Dependency Report

Page 74: Lorraine JUG (1st June, 2010) - Maven

74

Sonar, a quality dashboard

Page 75: Lorraine JUG (1st June, 2010) - Maven

75

Sonar, analyze your project

Page 76: Lorraine JUG (1st June, 2010) - Maven

76

Sonar, Continuous Improvement ?

Page 77: Lorraine JUG (1st June, 2010) - Maven

77

CONTINUOUS INTEGRATION Apache Maven

Page 78: Lorraine JUG (1st June, 2010) - Maven

78

Continuous Integration

•  Setup a continuous integration server to : » Have a neutral and unmodified environment to run your

tests » Quickly react when

•  The build fails (compilation failure for example) •  A test fails •  A quality metric is bad

» Continuously improve the quality of your project and your productivity

•  Many products » Hudson, Bamboo, TeamCity, Continuum, Cruisecontrol,

Page 79: Lorraine JUG (1st June, 2010) - Maven

79

Hudson, how the weather is ?

Page 80: Lorraine JUG (1st June, 2010) - Maven

80

Hudson : build, test, check

Page 81: Lorraine JUG (1st June, 2010) - Maven

81

IDE Apache Maven

Page 82: Lorraine JUG (1st June, 2010) - Maven

82

Eclipse

•  Integration from maven (eclipse:eclipse) » Allow many customizations » Support many versions/variants of eclipse » Support many usages (ear …) » Doesn’t support projects with “pom” packaging » Few support from dev team » Many bugs in classpath management » Asynchronous

•  You have to regenerate and reload project each time you change a POM)

Page 83: Lorraine JUG (1st June, 2010) - Maven

83

Eclipse

•  Integration from eclipse (m2eclipse) » Synchronous » Nice UI and services to edit POMs » Support projects with “pom” packaging » Doesn’t support all usages like EAR with WTP » Doesn’t support very well a large number of

modules » Slow down eclipse on large projects because of a

lack of support of incremental build in Maven 2.x and its plugins

Page 84: Lorraine JUG (1st June, 2010) - Maven

84

Eclipse (m2eclipse)

Page 85: Lorraine JUG (1st June, 2010) - Maven

85

Eclipse (m2eclipse)

Page 86: Lorraine JUG (1st June, 2010) - Maven

86

Eclipse (m2eclipse)

Page 87: Lorraine JUG (1st June, 2010) - Maven

87

Idea IntelliJ

Page 88: Lorraine JUG (1st June, 2010) - Maven

88

Netbeans

Page 89: Lorraine JUG (1st June, 2010) - Maven

GOOD & BAD PRACTICES Apache Maven

Page 90: Lorraine JUG (1st June, 2010) - Maven

90

KISS Apache Maven

Page 91: Lorraine JUG (1st June, 2010) - Maven

91

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

Page 92: Lorraine JUG (1st June, 2010) - Maven

92

PROJECT ORGANIZATION GOOD & BAD PRACTICES Apache Maven

Page 93: Lorraine JUG (1st June, 2010) - Maven

93

Project bad practices

Page 94: Lorraine JUG (1st June, 2010) - Maven

94

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 …

Page 95: Lorraine JUG (1st June, 2010) - Maven

95

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 …

Page 96: Lorraine JUG (1st June, 2010) - Maven

96

POM GOOD & BAD PRACTICES Apache Maven

Page 97: Lorraine JUG (1st June, 2010) - Maven

97

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

Page 98: Lorraine JUG (1st June, 2010) - Maven

98

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.

Page 99: Lorraine JUG (1st June, 2010) - Maven

99

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.

Page 100: Lorraine JUG (1st June, 2010) - Maven

100

DEVELOPMENT GOOD & BAD PRACTICES Apache Maven

Page 101: Lorraine JUG (1st June, 2010) - Maven

101

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

Page 102: Lorraine JUG (1st June, 2010) - Maven

102

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)

Page 103: Lorraine JUG (1st June, 2010) - Maven

USECASES Apache Maven

Page 104: Lorraine JUG (1st June, 2010) - Maven

104

SECURE YOUR CREDENTIALS Apache Maven

Page 105: Lorraine JUG (1st June, 2010) - Maven

105

Secure your credentials

-  arnaud@leopard:~$ mvn --encrypt-master-password toto {dZPuZ74YTJ0HnWHGm4zgfDlruYQNda1xib9vAVf2vvY=}

  Generate a private key

<settingssecurity>!

<master>{dZPuZ74YTJ0HnWHGm4zgfDlruYQNda1xib9vAVf2vvY=}</master>!

</settingssecurity>!

●  We save the private key in ~/.m2/settings-security.xml

Page 106: Lorraine JUG (1st June, 2010) - Maven

106

Secure your credentials

-  <settingsSecurity> <relocation>/Volumes/ArnaudUsbKey/secure/settings-security.xml</relocation> </settingsSecurity>!

●  You can move this key to another drive ~/.m2/settings.xml

●  You create an encrypted version of your server password

-  arnaud@mbp-arnaud:~$ mvn --encrypt-password titi{SbC9Fl2jA4oHZtz5Fcefp2q1tMXEtBkz9QiKljPiHss=}!

●  You register it in your settings

-  <settings> ... <servers> ... <server> <id>mon.server</id> <username>arnaud</username> <password>{SbC9Fl2jA4oHZtz5Fcefp2q1tMXEtBkz9QiKljPiHss=}</password> </server> ... </servers> ... </settings>!

Page 107: Lorraine JUG (1st June, 2010) - Maven

107

BUILD A PART OF YOUR PROJECT Apache Maven

Page 108: Lorraine JUG (1st June, 2010) - Maven

108

Using Reactor Options

•  Options added in maven 2.1 •  Available in 2.0.x with the maven-reactor-plugin

» But syntax is longer •  Allow to control what you want to build in your

project

Page 109: Lorraine JUG (1st June, 2010) - Maven

109

Using Reactor Options

-  arnaud@mbp-arnaud:~$ mvn install [INFO] ------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] Project ....................... SUCCESS [2.132s] [INFO] ModuleA ....................... SUCCESS [5.574s] [INFO] ModuleB ....................... SUCCESS [0.455s] [INFO] ModuleC ....................... SUCCESS [0.396s] [INFO] ModuleD ....................... SUCCESS [0.462s] [INFO] ModuleE ....................... SUCCESS [0.723s] [INFO] ModuleF ....................... SUCCESS [0.404s]!

  Builds everything from A to F

Page 110: Lorraine JUG (1st June, 2010) - Maven

110

Using Reactor Options

-  arnaud@mbp-arnaud:~$ mvn install –pl moduleE,moduleB [INFO] ------------------------------------------- [INFO] Reactor Summary: [INFO] [INFO] ModuleB .................. SUCCESS [2.774s] [INFO] ModuleE .................. SUCCESS [1.008s]

  Builds only modules B and E   Following dependencies order   -pl --project-list: Build the

specified reactor projects instead of all projects

Page 111: Lorraine JUG (1st June, 2010) - Maven

111

Using Reactor Options

-  arnaud@mbp-arnaud:~$ mvn install –pl moduleD -am [INFO] ------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] ModuleA ................. SUCCESS [4.075s] [INFO] ModuleB ................. SUCCESS [0.468s] [INFO] ModuleC ................. SUCCESS [0.354s] [INFO] ModuleD ................. SUCCESS [0.384s]

  Builds module D (-pl) and all modules it uses as dependencies

  -am --also-make: If a project list is specified, also make projects that the list depends on

  Usecase : Build all modules required for a war, ear, …

Page 112: Lorraine JUG (1st June, 2010) - Maven

112

Using Reactor Options

-  arnaud@mbp-arnaud:~$ mvn install –pl moduleD -amd [INFO] ------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] ModuleD ................. SUCCESS [4.881s] [INFO] ModuleE ................. SUCCESS [0.478s] [INFO] ModuleF ................. SUCCESS [0.427s]

  Builds module D (-pl) and all modules which depend on it

  -amd --also-make-dependents: If a project list is specified, also make projects that depend on projects on the list

  Usecase : Check that a change in a module didn’t break others which are using it

Page 113: Lorraine JUG (1st June, 2010) - Maven

113

Using Reactor Options

-  arnaud@mbp-arnaud:~$ mvn install –rf moduleD [INFO] ------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] ModuleD ................. SUCCESS [9.707s] [INFO] ModuleE ................. SUCCESS [0.625s] [INFO] ModuleF ................. SUCCESS [0.679s] [INFO] Project ................. SUCCESS [2.467s]

  Restarts all the build from module D (-rf)

  -rf,--resume-from <arg> : Resume reactor from specified project

  Usecase : The build failed a 1st time in module D, you fixed it, and restart the build were it was to end it.

Page 114: Lorraine JUG (1st June, 2010) - Maven

114

RELEASE YOUR PROJECT Apache Maven

Page 115: Lorraine JUG (1st June, 2010) - Maven

115

Release of a webapp in 2002

•  Limited usage of eclipse » No WTP (Only some features in WSAD), » No ability to produce WARs

Page 116: Lorraine JUG (1st June, 2010) - Maven

116

Release of a webapp in 2002

•  Many manual tasks » Modify settings files » Package JARs » Copy libraries (external and internal) in a « lib »

directory » Package WAR (often with a zip command) » Tag the code (CVS) » Send the package on the integration server using

FTP » Deploy the package with AS console

Page 117: Lorraine JUG (1st June, 2010) - Maven

117

Release of a webapp in 2002

•  One problem : The are always problems » Error in config files » Missing dependencies »  Missing file »  Last minute fix which created a bug »  And many other possibilies ..

•  How long did it take ? » When everything is ok :

15 minutes » When there’s a

problem : ½ day or more

Page 118: Lorraine JUG (1st June, 2010) - Maven

118

Maven Release Plugin

•  Automates the release process from tagging sources to binaries delivery

•  Release plugin main goals: » Prepare : To update maven versions and

information in POMs and tag the code » Perform : To deploy binaries in a maven repository

•  After that you can just automate the deployment on the AS using cargo for example.

Page 119: Lorraine JUG (1st June, 2010) - Maven

119

Maven Release Plugin

Page 120: Lorraine JUG (1st June, 2010) - Maven

120

Configuration and Prerequisites

•  Project version (must be a SNAPSHOT version) •  Dependencies and plugins versions mustn’t be

SNAPSHOTs

Page 121: Lorraine JUG (1st June, 2010) - Maven

121

Troubleshooting Releases

•  Common errors during release: » Build with release profile was tested before and fails » Local modifications » Current version is not a SNAPSHOT » SNAPSHOTs in dependencies and/or plugins » Missing some configuration (scm, distribMgt, …) » Tag already exists » Unable to deploy project to the Repository » Connection problems

Page 122: Lorraine JUG (1st June, 2010) - Maven

122

SCM configuration

SCM binaries have to be in the PATH SCM credentials have to already be stored or you

have to pass them in command line with : –Dusername=XXX –Dpassword=XXX

<scm>! <connection>scm:svn:http://svn.acme.com/myproject/trunk</connection>! <developerConnection>scm:svn:https://svn.acme.com/myproject/trunk</developerConnection>! <url>http://fisheye.acme.com/browse/myproject/trunk</url>!</scm>!

Page 123: Lorraine JUG (1st June, 2010) - Maven

123

Distribution Management

•  Where you want to upload released binaries » The url of a repository dedicated for your project/

corporate maven deliveries in your repository manager

<project>! <distributionManagement>! <repository>! <id>repository.acme.com</id>! <url>${acme.releases.repo.url}</url>! </repository>! . . .! </distributionManagement>! . . . ! <properties>! <acme.releases.repo.url>http://repo.acme.com/acme-releases</acme.releases.repo.url>! . . .! </properties>!</project>!

This id will be used in user’s maven settings (~/.m2/settings.xml)

Often useful to have a property to test the release process on a fake repository, to validate a repo manager ...

Page 124: Lorraine JUG (1st June, 2010) - Maven

124

Repository credentials

•  One server entry is required per different repository id in distribution management of projects

•  In a corporate environment, use a unique id for all repositories hosted on repository managers using same credentials (corporate LDAP …)

<settings>! ...! <servers>! <server>! <id>repository.acme.com</id>! <username>aheritier</username>! <password>{ABCDEFGHIJKLMNOPQRSTUVWYZ}</password>! </server>! ...! </servers>! ...!</settings>!

This id is the one defined in distributionManagement entry of the project to release

Page 125: Lorraine JUG (1st June, 2010) - Maven

125

Default Release Profile in Super POM

•  This profile is used when you generate binaries of the release with “mvn release:perform”

•  By default, generates sources and javadocs jars for each module.

<profile>! <id>release-profile</id>! <activation>! <property>! <name>performRelease</name>! <value>true</value>! </property>! </activation>! <build>! <plugins>! ...! </plugins>! </build>!</profile>!

Configuration to generate sources and javadoc jars with basic setting

This activation could be used in profiles you want to activate in the release process

Page 126: Lorraine JUG (1st June, 2010) - Maven

126

Custom release profile <project>! ...! <build>! <pluginManagement>! <plugins>! <plugin>! <groupId>org.apache.maven.plugins</groupId>! <artifactId>maven-release-plugin</artifactId>! <version>2.0</version>! <configuration>! <useReleaseProfile>false</useReleaseProfile>! <releaseProfiles>myreleaseprofile</releaseProfiles>! </configuration>! </plugin>! </plugins>! </pluginManagement>! </build>! ...! <profiles>! <profile>! <id>myreleaseprofile</id>! <build>! ...! </build>! </profile>! </profiles>! ...!</project>!

Customize the behavior of the build for a release Take care to test is before the release !!

Use our customized profile Don’t use the default profile

Our customized profile

Page 127: Lorraine JUG (1st June, 2010) - Maven

127

SETUP A GLOBAL MIRROR Apache Maven

Page 128: Lorraine JUG (1st June, 2010) - Maven

128

Why should we setup a global mirror ?

•  To simplify users and projects settings •  To control where binaries are coming from

» To not rely on project’s repositories » To use only the corporate repository manager

•  To improve build performances » By reducing the number of requests to find a

missing artefact

Page 129: Lorraine JUG (1st June, 2010) - Maven

129

How should we setup a global mirror ? <setting>

<mirrors> <mirror> <id>global-mirror</id> <mirrorOf>external:*</mirrorOf> <url>http://repo.acme.com/public</url> </mirror> </mirrors> <profiles> <profile> <id>mirror</id> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>mirror</activeProfile> </activeProfiles> </settings> !

Send all requests to this url

Enable snapshots

make the profile active all the time

Use « central » id to override default maven configuration

Page 130: Lorraine JUG (1st June, 2010) - Maven

130

CONCLUSION Apache Maven

Page 131: Lorraine JUG (1st June, 2010) - Maven

131

Conclusion

•  Today, Maven is widely adopted in corporate environments,

•  It provides many services, •  It has an important and really active community of

users and developers •  Many resources to learn to use it and a

professional support are available •  A product probably far from being perfect but on

rails for the future. Maven 3.0 is a new start. •  Many things to do

» We need you !

Page 132: Lorraine JUG (1st June, 2010) - Maven

132

QUESTIONS ? Apache Maven

Page 133: Lorraine JUG (1st June, 2010) - Maven

133

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/

Page 134: Lorraine JUG (1st June, 2010) - Maven

TO GO FURTHER … Apache Maven

Page 135: Lorraine JUG (1st June, 2010) - Maven

135

DOCUMENTATIONS Apache Maven

Page 136: Lorraine JUG (1st June, 2010) - Maven

136

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

Page 137: Lorraine JUG (1st June, 2010) - Maven

137

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

Page 138: Lorraine JUG (1st June, 2010) - Maven

138

Books

•  Sonatype / O’Reilly : » The Definitive Guide » http://

www.sonatype.com/books

» Free download » Available in several

languages

Page 139: Lorraine JUG (1st June, 2010) - Maven

139

Books

•  Exist Global » Better builds with

Maven » http://

www.maestrodev.com/better-build-maven

» Free download

Page 140: Lorraine JUG (1st June, 2010) - Maven

140

SUPPORT Apache Maven

Page 141: Lorraine JUG (1st June, 2010) - Maven

141

Support

•  Mailing lists » http://maven.apache.org/mail-lists.html

•  IRC » irc.codehaus.org - #maven

•  Forums » http://www.developpez.net/ forum maven » In French

•  Dedicated support » Sonatype and many others companies