Maven
Maven
fr/donsez/cours
Apache Maven 2
Didier DONSEZ
Université Joseph Fourier – Grenoble 1
PolyTech’Grenoble – LIG/ADELE
[email protected]
[email protected]
[email protected]
29/03/2010
Licence
Cette présentation est couverte
par le contrat Creative Commons By NC ND
http://creativecommons.org/licenses/by-nc-nd/2.0/fr/
Didier Donsez, 2007-2010, Maven
2
29/03/2010
Au sommaire
Motivation
Installation
Cycle de vie
Structure d’un projet Maven
Plugins Maven
Développement d’un plugin
Bibliographie et Webographie
Didier Donsez, 2007-2010, Maven
3
29/03/2010
Motivations
Rappel sur ANT
Remplaçant de make (pour les développements Java)
NAnt pour .NET
Séquenceur de taches (regroupées en cible)
Graphe de dépendance de « cibles »
Très grand nombre de taches développées
Défaut
Pas de structure « standard » de projet
Pas de cycle de vie « standard » d’un projet
Didier Donsez, 2007-2010, Maven
Motivations de Maven
Modèle abstrait de projet (POM)
Orienté objet, héritage
Séparation de préoccupations
Cycle de vie standard
Séquencement d’états (goal) standards
Action des plugins en fonction des états
Structure « standard » de projet
Nommage standard des variables (src.dir, …)
Gestion automatique des dépendances avec d’autres projets
Chargement des MAJ
Dépôts des projets
Didier Donsez, 2007-2010, Maven
5
29/03/2010
Installation
Télécharger la distribution binaire
http://maven.apache.org
Dézipper dans un répertoire
Positionner les variables d’environnement
Évites d’être
set JAVA_HOME=c:\j2sdk1.x.y
ennuyer par des
set MAVEN_HOME=c:\maven-2.z.w VM « parasites »
set PATH=%JAVA_HOME%\bin;%MAVEN_HOME%\bin
mvn –version
mvn --help
(éventuellement) Configurer ~/.m2/settings.xml
Didier Donsez, 2007-2010, Maven
6
29/03/2010
mvn --help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-q,--quiet Quiet output - only show errors
-C,--strict-checksums Fail the build if checksums don't match
-c,--lax-checksums Warn if checksums don't match
-P,--activate-profiles Comma-delimited list of profiles to activate
-ff,--fail-fast Stop at first failure in reactorized builds
-fae,--fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue
-B,--batch-mode Run in non-interactive (batch) mode
-fn,--fail-never NEVER fail the build, regardless of project result
-up,--update-plugins Synonym for cpu
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Don't use ~/.m2/plugin-registry.xml for plugin versions
-U,--update-snapshots Forces a check for updated releases and snapshots on remote repositories
-cpu,--check-plugin-updates Force upToDate check for any relevantregistered plugins
-npu,--no-plugin-updates Suppress upToDate check for any relevant registered plugins
Didier Donsez, 2007-2010, Maven
Le modèle de projet
(POM pour Project Object Model)
Description d’un projet indépendante des actions à accomplir
Orienté objet héritage du modèle
Exemple
<project> Identifiant (unique) du projet :
<modelVersion>4.0.0</modelVersion> Identifiant de l’artifact produit
<groupId>com.mycompany.app</groupId> type du projet:
<artifactId>my-app</artifactId> pom, jar, war, ear, bundle, …
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging> dépendances du projet envers
<dependencies> d’autres projets (artifact)
constitue le $CLASSPATH
<dependency>
id d’une dépendance
<groupId>junit</groupId>
version peut être un intervalle
<artifactId>junit</artifactId>
Didier Donsez, 2007-2010, Maven
Dependances
Concerne les artifacts comme les plugins
Résolution transitive
<project> …
<groupId>com.mycompany.app</groupId> mybundle-1.0.0
<artifactId>mybundle</artifactId>
<version>1.0.0</version> … dependency
<dependencies> declaration
<dependency>
<groupId>org.osgi</groupId> compendium-4.0.0
<artifactId>compendium</artifactId>
<version>4.0.0</version> transitive
</dependency> resolution
Didier Donsez, 2007-2010, Maven
</dependencies>
… </project>
core-4.0.0
Portée transistive
10
29/03/2010
mvn package
Didier Donsez, 2007-2010, Maven
Code
Source validate generate-test-sources pre-integration-test
initialize process-test-sources integration-test
generate-test-resources post-integration-test
generate-sources
process-test-resources
process-sources
test-compile
generate-resources
Didier Donsez, 2007-2010, Maven
test
process-resources verify
compile install
process-classes package deploy
artifact 14
29/03/2010
package jar:jar
Remarque
mvn resources:resources compiler:compile resources:testResources compiler:testCompile
Didier Donsez, 2007-2010, Maven
15
29/03/2010
Rappel
Numérotation des versions
Schéma
<major>.<mini>[.<micro>][-<qualifier>[-<buildnumber>]]
Incrément
Major : changement majeur
pas de retro-compatibilité (descendante) garantie
Mini : ajouts fonctionnels
retro-compatibilité garantie
Micro : maintenance corrective (bug fix)
Qualificateurs
SNAPSHOT (Maven) : version en évolution
alpha1 : version alpha (très instable et incomplète)
beta1, b1, b2 : version beta (instable)
rc1, rc2 : release candidate
m1, m2 : milestone
ea : early access
20081014123459001 : date du build
jdk5 : dépendance avec une arch, un os, un langage
Ordre sur les versions
Didier Donsez, 2007-2010, Maven
17
29/03/2010
Versionnement
Snapshot
A snapshot in Maven is an artifact which has been prepared using
the most recent sources available. … Specifying a snapshot
version for a dependency means that Maven will look for new
versions of that dependency without you having to manually specify
a new version.
mvn -U command line option to force the search for updates.
Dépendances
Didier Donsez, 2007-2010, Maven
19
29/03/2010
IDEs
eclipse, netbeans, idea
Autres
exec, jdepend, castor, cargo, jetty, native, sql, taglist, javacc, obr …
http://maven.apache.org/plugins/, http://mojo.codehaus.org/plugins.html, …
21
29/03/2010
<mainClass>${artifactId}.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
22
29/03/2010
Profils
Motivation
Améliorer la portabilité des projets par rapport aux
environnements
Différents JVM, versions de Java, serveurs JEE, SGBD,
développement versus production
Créer des variations (=profils) de projets
Elément <profile> du build
Contient les variations de plugins et entre les plugins
Activation du profil
Didier Donsez, 2007-2010, Maven
Commande
mvn --reactor clean install
Pour la construction globale
26
29/03/2010
mvn site:site
Didier Donsez, 2007-2010, Maven
27
29/03/2010
Dépôts de projets
Local ~/.m2/repository
Projets (dont artifacts) installés
localement
mvn install
mvn install:install-file
Caches des projets (artifacts) téléchargés
depuis les dépots distants
Listés dans les POM et settings.xml
Distants
Dépôts d’entreprise
Cache de dépôts
Didier Donsez, 2007-2010, Maven
Dépôts publiques
Structure
Nommage hiérarchique
${groupId}.replace('.','/') / ${articfactId} / ${version}
28
29/03/2010
Dépôts publiques
Les principaux
Apache Maven Central
http://repo1.maven.org/maven2/
Plus de 20000 artifacts décrits (en 2007)
Tous en licence ASL v2
CodeHaus
http://www.codehaus.org
Dependance vers d’autres licences (BSD, …)
Didier Donsez, 2007-2010, Maven
29
29/03/2010
Web Servers
project.xml (POM)
Download artifact
1 Get dependencies
3
Maven core
HTTP
Remote
Repositories
2 4 (listed in
Local ~/.m2/settings.xml)
Checks if Repository Save artifact in local
Didier Donsez, 2007-2010, Maven
JVM 1 JVM 2
R1: La mise à jour du dépôt local est journalière (sauf si mvn –U)
R2: Les plugins sont recherchés et mis à jour de la même façon
30
D’après Vincent Massol
29/03/2010
<directory>src/main/resources</directory>
<filtering>true</filtering>
# src/main/resources/application.properties
</resource>
message=${my.filter.value}
</resources> application.name=${project.name}
</build> application.version=${project.version}
31
29/03/2010
Archetype
Construction initial d’un projet Maven
En fonction d’un type de projet T
T= quickstart, archetype, bundles, j2ee-simple, marmalade-
mojo, mojo, plugin, plugin-site, portlet, profiles, simple, site,
site-simple, webapp, …
Exemple
mvn archetype:create
mode interactif
mvn archetype:create
-DgroupId=demo.maven
Didier Donsez, 2007-2010, Maven
-DartifactId=hello
-Dversion=0.1.0-SNAPSHOT
-DarchetypeGroupId=org.apache.maven.archetypes
-DarchetypeArtifactId=maven-archetype-quickstart
32
29/03/2010
Archetypes personnalisés
Possibilité de créer ses propres archetypes
de zero
mvn archetype:create
-DarchetypeGroupId=org.apache.maven.archetypes
-DarchetypeArtifactId=maven-archetype-archetype
-DgroupId=com.mycompany
-DartifactId=my-archetype
33
29/03/2010
Développement de plugins
Plugin = { <goal,MOJO> }
MOJO = Maven POJO
Annotations XDocLet
Langages
Java et Groovy (pour le scripting)
D’autres possibles …
Déploiement
Didier Donsez, 2007-2010, Maven
Artifact Maven
Utilise les mécanismes de déploiement (version, dépendances, …)
Dépôts de plugins
http://maven.apache.org/plugins/, http://repository.codehaus.org/
34
29/03/2010
Développement de plugins
Exemple (i)
package sample.plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
/** phase et but durant laquelle
* Says "Hi" to the user. execute() est appelé
* @goal sayhi
* @phase compile
*/ paramètre renseigné dans
public class GreetingMojo extends AbstractMojo { <configuration>
/** The greeting to display.
* @parameter alias="message" expression="Hello, world (from ${project.groupId}:${project.artifactId})" */
private String greeting;
Integer, …, String, List, Properties,
Map, Object, File, URL, …
/** The classpath.
Didier Donsez, 2007-2010, Maven
* @parameter expression="${project.compileClasspathElements}"
* @required
* @readonly */ paramètre issue du pom
private List classpathElements;
Développement de plugins
Exemple (ii)
Dans le POM
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>maven-hello-plugin</artifactId>
<configuration>
<message>Welcome</message>
</configuration>
</plugin>
Didier Donsez, 2007-2010, Maven
</plugins>
</build>
Exécution
mvn sample.plugin:maven-hello-plugin:sayhi
36
29/03/2010
37
29/03/2010
executable="${basedir}/src/main/sh/do-something.sh" failonerror="true">
<arg line="arg1 arg2 arg3 arg4" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin> 38
29/03/2010
39
29/03/2010
</artifact:dependencies>
<javac ...>
<classpath refid="dependency.classpath" />
...
</javac>
40
29/03/2010
Structure du projet
src/main/java
src/main/c
src/main/cpp
src/main/cs
src/main/php
Didier Donsez, 2007-2010, Maven
src/main/vb
…
Plugins
maven-antrun-plugin (org.apache.maven.plugins:)
pour la récupération de tâches Ant patrimoniales
native-maven-plugin (org.codehaus.mojo:)
42
29/03/2010
Misc
Maven SCM
Plugin offrant une API commun vers les principaux SCM
Commandes
Changelog - command to show the source code revisions
Checkin - command for commiting changes
Checkout - command for getting the source code
Diff - command for showing the difference of the working copy with the remote ones
Edit - command for starting edit on the working copy
Status - command for showing the scm status of the working copy
Tag - command for tagging the certain revision
UnEdit - command for to stop editing the working copy
Update - command for updating the working with the latest changes
Validate - validates the scm information on the pom
Supported SCM
Subversion, CVS, Starteam, Clearcase, Perforce, bazaar
Maven Continuum
continuous integration (JEE-based) server for building Java based projects.
Schedulable projects: Maven 1, Maven 2, Ant, Shell scripts
Notifications : Mail and IM (IRC, Jabber, MSN)
Didier Donsez, 2007-2010, Maven
Maven Archiva
Repository manager (search, security, reporting, …)
Maven Wagon
Outil de transfert des artifacts vers des dépots (distants ou locaux)
File, HTTP, HTTP lightweight, FTP, SSH/SCP, WebDAV, SCM
43
29/03/2010
Misc
Apache Ivy
Gestionnaire de dépendances (pour projet Ant)
Sous gestionnaire pour des dépôts Maven (locaux ou distants)
Didier Donsez, 2007-2010, Maven
44
29/03/2010
M2Eclipse
Plugin Eclipse pour Maven
Création de projets
Wizard, Archetypes
Edition du POM
Affichage graphique
Recherche de dépendances
Depuis les dépôts local et distants
Ajout des dépendances du POM
au .classpath
org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER
Didier Donsez, 2007-2010, Maven
45
29/03/2010
M2Eclipse
Plugin Eclipse pour Maven
Didier Donsez, 2007-2010, Maven
46
29/03/2010
Maven Continuum
http://maven.apache.org/continuum/
MSN)
Release management
SCM support
Didier Donsez, 2007-2010, Maven
47
29/03/2010
Maven Continuum
Console
Didier Donsez, 2007-2010, Maven
48
29/03/2010
Divers
Recherche d’artifacts
http://www.mvnrepository.com/
Didier Donsez, 2007-2010, Maven
49
29/03/2010
Misc
Tree Surgeon (Maven pour .NET ?)
http://confluence.public.thoughtworks.org/display/TREE/Tree+Surgeon
« Tree Surgeon est un outil Open Source édité par la
société de Martin Fowler Thoughtworks. "It is a tool that
automates the process of establishing a directory
structure with source code stubs and supporting
infrastructure in a consistent manner. (...) It supports
tools like NAnt and NUnit by generating build files and
unit tests as part of the automated process. . Même si
Didier Donsez, 2007-2010, Maven
Maven 3.x
Any-source POM (json, groovy, …)
Versionless parent elements
Mixin : a composition of POMs
Better IDE integration
Error reporting
Codes d’erreur commune
Lifecycle extension points
Plugin extension points
example: war extends jar
Incremental build support
Queryable lifecycle
comportement en fonction du lifecycle calculé au démarrage
Extensible reporting
Didier Donsez, 2007-2010, Maven
Bibliographie et Webographie
Web
Site Maven, http://maven.apache.org
Exemples et exercices
http://www-adele.imag.fr/users/Didier.Donsez/cours/tpmvn
Complément
http://www-adele.imag.fr/users/Didier.Donsez/cours/coursjavaoutil.pdf
Didier Donsez, 2007-2010, Maven
53
29/03/2010
Bibliographie et Webographie
http://maven.apache.org/articles.html
Ouvrages
Maven: The Definitive Guide
http://www.sonatype.com/book/maven-user-guide.pdf
En français http://www.maven-definitive-guide.fr/
John Casey, Vincent Massol, Brett Porter, Carlos Sanchez, Jason
van Zyl, Better Builds with Maven, Publisher Mergere Library Press,
March 2006
(PDF gratuit en ligne)
Vraiment bien, mais c’est préférable de connaître Maven auparavant !
Vincent Massol, Tim O'Brien, Maven: A Developer's Notebook,
Didier Donsez, 2007-2010, Maven
54
29/03/2010
55
29/03/2010