SlideShare une entreprise Scribd logo
1  sur  27
© 2019-2020 – MAVEN
MAVEN
mhassini@gmail.com
© 2019-2020 – MAVEN
PLAN DU COURS
2
– C’est Quoi Maven?
– Création d’un Projet Maven
– Balises du POM.XML
– Arborescence Standard
– Buts (Goals)
– TP : Projet avec Maven (JAR)
– TP : Projet Web Avec Maven (WAR)
© 2019-2020 – MAVEN
C’EST QUOI MAVEN?
3
• Maven est un outil de construction de projets (build) open source
développé par la fondation Apache.
• Il permet de faciliter et d'automatiser certaines tâches de la gestion
d'un projet JavaEE.
© 2019-2020 – MAVEN
FONCTIONNALITÉS
4
• Fonctionnalités
– Automatisation de tâches récurrentes
– Construction, Compilation des projets
– Gestion des dépendances
– Génération des livrables
– Génération de la documentation et de rapports
– Déploiement d'applications
• Modèle de projet basé sur des conventions (POM)
– Configuration basée sur le format XML
© 2019-2020 – MAVEN
INSTALLATION DE MAVEN
5
• Maven entant que Plugin : Un plugin Maven est déjà installé par
défaut avec Eclipse.
• Maven en standalone (non traité dans notre cours).
© 2019-2020 – MAVEN
EXERCICE
6
• Comment savoir si Maven est bien installé, et quelle version
nous utilisons (Dans le cas où Maven est installé en tant que
Plugin dans JBoss Dev Studio)?
© 2019-2020 – MAVEN
EXERCICE
7
• En mode plugin :
© 2019-2020 – MAVEN
Suite du Cours
8
• Créer un projet MAVEN simple via le plugin Maven
• Maitriser l’arborescence standard du code et de ses ressources
• Maîtriser les différents buts (Goals) du cycle de vie d’un projet Maven
(la compilation, le test, le packaging d’une application, …)
• Installer une application dans un Repository local
• Gérer les dépendances (bibliothèques) d’un projet donné
• Exécuter les tests unitaires automatiquement
© 2019-2020 – MAVEN
CRÉATION D’UN PROJET MAVEN
9
© 2019-2020 – MAVEN
BALISES DU POM.XML
10
• pom.xml : Project Model Object
• project : Balise racine de tous les fichiers pom.xml.
• modelVersion : Version de POM utilisée.
• groupId : Identifier un groupe qui a créé le projet. Ex: org.apache.
• artifactId : Nom unique utilisé pour nommer l’artifacts à construire.
• packaging : Type de packaging du projet ( ex. : JAR, WAR, EAR...).
• version : Version de l'artifact généré par le projet.
• name : Nom du projet.
• description : Description du projet.
• dependencies : balise permettant de gérer les dépendances.
• archetype : Template de Projet.
© 2019-2020 – MAVEN
ARBORESCENCE STANDARD
11
© 2019-2020 – MAVEN
PREMIÈRES COMMANDES
12
• Assurez vous que vous accès à Internet, Puis lancer les commandes suivantes clean /
install avec JBoss Dev Studio :
• Ceci va mettre à jour votre Repository local avec l’ensemble des plugin et
dépendances nécessaires pour que le bon fonctionnement de Maven.
• Partager votre Repository avec vos collègues qui n’ont pas accès à internet.
© 2019-2020 – MAVEN
ARBORESCENCE STANDARD
13
• pom.xml : le fichier de configuration du projet
• /src : code source et fichiers source principaux
• /src/main/java : code source java
• /src/main/resources : fichiers de ressources (images, fichiers config...)
• /src/main/webapp : webapp du projet
• /src/test : fichiers de test
• /src/test/java : code source Java de test
• /src/test/resources : fichiers de ressources de test
• /target : fichiers résultat, les binaires (du code et des tests), les
packages générés et les résultats des tests
© 2019-2020 – MAVEN
BUTS (GOALS)
14
• mvn compile : Créer les .class
• mvn test : Jouer les tests unitaires
• mvn package : Creation du livrable dans target.
• mvn install : Copie du livrable dans le Repository local :
~.m2repository...
• mvn deploy : Copie du livrable sur le repository distant
• mvn clean : Supprime le contenu du dossier target.
© 2019-2020 – MAVEN
BUTS (GOALS)
15
• Emplacement du livrable :
{emplacement Repository}/groupId/artifactId/version
• Nom du package (jar en général) : {artifactId}-{version}.{package}
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
© 2019-2020 – MAVEN
TP1 – Projet avec Maven (JAR)
16
• Créer un Projet Maven :
• Simple : sans archetype, type Jar, groupId : tn.esprit
• artefactId / nom / description : avec-maven
• package : tn.esprit
• Mettre à jour le pom.xml pour utiliser Java 1.8
• Créer le package : tn.esprit
• Créer la Classe : CallRestWebService (Voir Code Source pages suivantes).
• Ajouter dans le pom.xml les dépendances JSON et HTTPCLIENT (voir
dépendances pages suivantes).
• Créer le livrable avec Maven
• Exécuter la méthode main.
© 2019-2020 – MAVEN
TP1 – Projet avec Maven (JAR)
17
package tn.esprit;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/** @author Walid-YAICH */
public class CallRestWebService {
public static final String endpoint = "http://ip-api.com/json";
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(endpoint);
String ip = "not found";
© 2019-2020 – MAVEN
TP1 – Projet avec Maven (JAR)
18
try {
HttpResponse response = client.execute(request);
String jsonResponse = EntityUtils.toString(response.getEntity());
System.out.println("Response as String : " + jsonResponse);
JSONObject responseObj = new JSONObject(jsonResponse);
ip = responseObj.getString("query");
System.out.println("ip : " + ip);
} catch (IOException e) { e.printStackTrace(); }
}
}
© 2019-2020 – MAVEN
TP1 – Projet avec Maven (JAR)
19
<project …>
…
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
</project>
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
20
• Créer un nouveau projet de type Maven, simple (sans archetype)
• Projet de type WAR
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
21
• Corriger l’erreur et le warning ci-dessous :
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
22
• Correction de l’erreur « web.xml missing », tout projet web doit contenir un
fichier web.xml, cliquer sur « Generate … » pour le générer :
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
23
• Correction du warning « Java 1.5 », Pointer sur Java 8 :
• Ajouter dans pom.xml les propriétés suivantes :
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
• Puis, Faites un Maven Update.
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
24
• Faites un Maven Update.
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
25
• Ajouter une page web basique index.html :
© 2019-2020 – MAVEN
TP2 – Projet Web avec Maven (WAR)
26
• Déployer l’application sur Tomcat, et lancer l’URL :
http://localhost:18080/maven-web
© 2019-2020 – MAVEN
MAVEN
Si vous avez des questions, n’hésitez pas à nous
contacter : mhassini@gmail.com

Contenu connexe

Tendances

Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications Java
Antoine Rey
 
Présentation Maven
Présentation MavenPrésentation Maven
Présentation Maven
SOAT
 

Tendances (20)

Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications Java
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
Mohamed youssfi support architectures logicielles distribuées basées sue les ...
Mohamed youssfi support architectures logicielles distribuées basées sue les ...Mohamed youssfi support architectures logicielles distribuées basées sue les ...
Mohamed youssfi support architectures logicielles distribuées basées sue les ...
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
Support developpement applications mobiles avec ionic v3 et v4
Support developpement applications mobiles avec ionic v3 et v4Support developpement applications mobiles avec ionic v3 et v4
Support developpement applications mobiles avec ionic v3 et v4
 
Support de Cours JSF2 Première partie Intégration avec Spring
Support de Cours JSF2 Première partie Intégration avec SpringSupport de Cours JSF2 Première partie Intégration avec Spring
Support de Cours JSF2 Première partie Intégration avec Spring
 
Java entreprise edition et industrialisation du génie logiciel par m.youssfi
Java entreprise edition et industrialisation du génie logiciel par m.youssfiJava entreprise edition et industrialisation du génie logiciel par m.youssfi
Java entreprise edition et industrialisation du génie logiciel par m.youssfi
 
Servlets et JSP
Servlets et JSPServlets et JSP
Servlets et JSP
 
Présentation Maven
Présentation MavenPrésentation Maven
Présentation Maven
 
Support JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVCSupport JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVC
 
Docker
DockerDocker
Docker
 
cours j2ee -présentation
cours  j2ee -présentationcours  j2ee -présentation
cours j2ee -présentation
 
Sécurité des Applications Web avec Json Web Token (JWT)
Sécurité des Applications Web avec Json Web Token (JWT)Sécurité des Applications Web avec Json Web Token (JWT)
Sécurité des Applications Web avec Json Web Token (JWT)
 
Support NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDBSupport NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDB
 
Support Java Avancé Troisième Partie
Support Java Avancé Troisième PartieSupport Java Avancé Troisième Partie
Support Java Avancé Troisième Partie
 
Appalications JEE avec Servlet/JSP
Appalications JEE avec Servlet/JSPAppalications JEE avec Servlet/JSP
Appalications JEE avec Servlet/JSP
 
20080311 - Paris Vi Master STL TA - Initiation Maven
20080311 - Paris Vi Master STL TA - Initiation Maven20080311 - Paris Vi Master STL TA - Initiation Maven
20080311 - Paris Vi Master STL TA - Initiation Maven
 
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka StreamsTraitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
 
Spring security
Spring securitySpring security
Spring security
 
Site JEE de ECommerce Basé sur Spring IOC MVC Security JPA Hibernate
Site JEE de ECommerce  Basé sur Spring IOC MVC Security JPA HibernateSite JEE de ECommerce  Basé sur Spring IOC MVC Security JPA Hibernate
Site JEE de ECommerce Basé sur Spring IOC MVC Security JPA Hibernate
 

Similaire à Maven

0251-formation-java-programmation-objet.pdf
0251-formation-java-programmation-objet.pdf0251-formation-java-programmation-objet.pdf
0251-formation-java-programmation-objet.pdf
Ombotimbe Salifou
 
Play framework - Human Talks Grenoble - 12.02.2013
Play framework - Human Talks Grenoble - 12.02.2013Play framework - Human Talks Grenoble - 12.02.2013
Play framework - Human Talks Grenoble - 12.02.2013
Xavier NOPRE
 
Intégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec JenkinsIntégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec Jenkins
Hugo Hamon
 

Similaire à Maven (20)

JCertif 2012 : Maven par la pratique
JCertif 2012 : Maven par la pratiqueJCertif 2012 : Maven par la pratique
JCertif 2012 : Maven par la pratique
 
Gradle_LyonJUG
Gradle_LyonJUGGradle_LyonJUG
Gradle_LyonJUG
 
gradle_nantesjug
gradle_nantesjuggradle_nantesjug
gradle_nantesjug
 
Présentation1
Présentation1Présentation1
Présentation1
 
Gradle_ToursJUG
Gradle_ToursJUGGradle_ToursJUG
Gradle_ToursJUG
 
Gradle_ToulouseJUG
Gradle_ToulouseJUGGradle_ToulouseJUG
Gradle_ToulouseJUG
 
Gradle_NormandyJUG
Gradle_NormandyJUGGradle_NormandyJUG
Gradle_NormandyJUG
 
0251-formation-java-programmation-objet.pdf
0251-formation-java-programmation-objet.pdf0251-formation-java-programmation-objet.pdf
0251-formation-java-programmation-objet.pdf
 
Maven/Ivy vs OSGi (Toulouse Jug 26-05-2011)
Maven/Ivy vs OSGi (Toulouse Jug 26-05-2011)Maven/Ivy vs OSGi (Toulouse Jug 26-05-2011)
Maven/Ivy vs OSGi (Toulouse Jug 26-05-2011)
 
Programmation Java
Programmation JavaProgrammation Java
Programmation Java
 
Gradle_BordeauxJUG
Gradle_BordeauxJUGGradle_BordeauxJUG
Gradle_BordeauxJUG
 
Octo Maven.pdf
Octo Maven.pdfOcto Maven.pdf
Octo Maven.pdf
 
Soiree Maven 2
Soiree Maven 2Soiree Maven 2
Soiree Maven 2
 
SLIDES-625.1.1-IDL-4-build tools maven.pdf
SLIDES-625.1.1-IDL-4-build tools maven.pdfSLIDES-625.1.1-IDL-4-build tools maven.pdf
SLIDES-625.1.1-IDL-4-build tools maven.pdf
 
Gradle_BreizJUG
Gradle_BreizJUGGradle_BreizJUG
Gradle_BreizJUG
 
Gradle
GradleGradle
Gradle
 
Play framework - Human Talks Grenoble - 12.02.2013
Play framework - Human Talks Grenoble - 12.02.2013Play framework - Human Talks Grenoble - 12.02.2013
Play framework - Human Talks Grenoble - 12.02.2013
 
Intégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec JenkinsIntégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec Jenkins
 
Presentation of GWT 2.4 (PDF version)
Presentation of GWT 2.4 (PDF version)Presentation of GWT 2.4 (PDF version)
Presentation of GWT 2.4 (PDF version)
 
Scub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libreScub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libre
 

Maven

  • 1. © 2019-2020 – MAVEN MAVEN mhassini@gmail.com
  • 2. © 2019-2020 – MAVEN PLAN DU COURS 2 – C’est Quoi Maven? – Création d’un Projet Maven – Balises du POM.XML – Arborescence Standard – Buts (Goals) – TP : Projet avec Maven (JAR) – TP : Projet Web Avec Maven (WAR)
  • 3. © 2019-2020 – MAVEN C’EST QUOI MAVEN? 3 • Maven est un outil de construction de projets (build) open source développé par la fondation Apache. • Il permet de faciliter et d'automatiser certaines tâches de la gestion d'un projet JavaEE.
  • 4. © 2019-2020 – MAVEN FONCTIONNALITÉS 4 • Fonctionnalités – Automatisation de tâches récurrentes – Construction, Compilation des projets – Gestion des dépendances – Génération des livrables – Génération de la documentation et de rapports – Déploiement d'applications • Modèle de projet basé sur des conventions (POM) – Configuration basée sur le format XML
  • 5. © 2019-2020 – MAVEN INSTALLATION DE MAVEN 5 • Maven entant que Plugin : Un plugin Maven est déjà installé par défaut avec Eclipse. • Maven en standalone (non traité dans notre cours).
  • 6. © 2019-2020 – MAVEN EXERCICE 6 • Comment savoir si Maven est bien installé, et quelle version nous utilisons (Dans le cas où Maven est installé en tant que Plugin dans JBoss Dev Studio)?
  • 7. © 2019-2020 – MAVEN EXERCICE 7 • En mode plugin :
  • 8. © 2019-2020 – MAVEN Suite du Cours 8 • Créer un projet MAVEN simple via le plugin Maven • Maitriser l’arborescence standard du code et de ses ressources • Maîtriser les différents buts (Goals) du cycle de vie d’un projet Maven (la compilation, le test, le packaging d’une application, …) • Installer une application dans un Repository local • Gérer les dépendances (bibliothèques) d’un projet donné • Exécuter les tests unitaires automatiquement
  • 9. © 2019-2020 – MAVEN CRÉATION D’UN PROJET MAVEN 9
  • 10. © 2019-2020 – MAVEN BALISES DU POM.XML 10 • pom.xml : Project Model Object • project : Balise racine de tous les fichiers pom.xml. • modelVersion : Version de POM utilisée. • groupId : Identifier un groupe qui a créé le projet. Ex: org.apache. • artifactId : Nom unique utilisé pour nommer l’artifacts à construire. • packaging : Type de packaging du projet ( ex. : JAR, WAR, EAR...). • version : Version de l'artifact généré par le projet. • name : Nom du projet. • description : Description du projet. • dependencies : balise permettant de gérer les dépendances. • archetype : Template de Projet.
  • 11. © 2019-2020 – MAVEN ARBORESCENCE STANDARD 11
  • 12. © 2019-2020 – MAVEN PREMIÈRES COMMANDES 12 • Assurez vous que vous accès à Internet, Puis lancer les commandes suivantes clean / install avec JBoss Dev Studio : • Ceci va mettre à jour votre Repository local avec l’ensemble des plugin et dépendances nécessaires pour que le bon fonctionnement de Maven. • Partager votre Repository avec vos collègues qui n’ont pas accès à internet.
  • 13. © 2019-2020 – MAVEN ARBORESCENCE STANDARD 13 • pom.xml : le fichier de configuration du projet • /src : code source et fichiers source principaux • /src/main/java : code source java • /src/main/resources : fichiers de ressources (images, fichiers config...) • /src/main/webapp : webapp du projet • /src/test : fichiers de test • /src/test/java : code source Java de test • /src/test/resources : fichiers de ressources de test • /target : fichiers résultat, les binaires (du code et des tests), les packages générés et les résultats des tests
  • 14. © 2019-2020 – MAVEN BUTS (GOALS) 14 • mvn compile : Créer les .class • mvn test : Jouer les tests unitaires • mvn package : Creation du livrable dans target. • mvn install : Copie du livrable dans le Repository local : ~.m2repository... • mvn deploy : Copie du livrable sur le repository distant • mvn clean : Supprime le contenu du dossier target.
  • 15. © 2019-2020 – MAVEN BUTS (GOALS) 15 • Emplacement du livrable : {emplacement Repository}/groupId/artifactId/version • Nom du package (jar en général) : {artifactId}-{version}.{package} <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
  • 16. © 2019-2020 – MAVEN TP1 – Projet avec Maven (JAR) 16 • Créer un Projet Maven : • Simple : sans archetype, type Jar, groupId : tn.esprit • artefactId / nom / description : avec-maven • package : tn.esprit • Mettre à jour le pom.xml pour utiliser Java 1.8 • Créer le package : tn.esprit • Créer la Classe : CallRestWebService (Voir Code Source pages suivantes). • Ajouter dans le pom.xml les dépendances JSON et HTTPCLIENT (voir dépendances pages suivantes). • Créer le livrable avec Maven • Exécuter la méthode main.
  • 17. © 2019-2020 – MAVEN TP1 – Projet avec Maven (JAR) 17 package tn.esprit; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONObject; /** @author Walid-YAICH */ public class CallRestWebService { public static final String endpoint = "http://ip-api.com/json"; public static void main(String[] args) { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(endpoint); String ip = "not found";
  • 18. © 2019-2020 – MAVEN TP1 – Projet avec Maven (JAR) 18 try { HttpResponse response = client.execute(request); String jsonResponse = EntityUtils.toString(response.getEntity()); System.out.println("Response as String : " + jsonResponse); JSONObject responseObj = new JSONObject(jsonResponse); ip = responseObj.getString("query"); System.out.println("ip : " + ip); } catch (IOException e) { e.printStackTrace(); } } }
  • 19. © 2019-2020 – MAVEN TP1 – Projet avec Maven (JAR) 19 <project …> … <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.1.1</version> </dependency> </dependencies> </project>
  • 20. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 20 • Créer un nouveau projet de type Maven, simple (sans archetype) • Projet de type WAR
  • 21. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 21 • Corriger l’erreur et le warning ci-dessous :
  • 22. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 22 • Correction de l’erreur « web.xml missing », tout projet web doit contenir un fichier web.xml, cliquer sur « Generate … » pour le générer :
  • 23. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 23 • Correction du warning « Java 1.5 », Pointer sur Java 8 : • Ajouter dans pom.xml les propriétés suivantes : <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> • Puis, Faites un Maven Update.
  • 24. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 24 • Faites un Maven Update.
  • 25. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 25 • Ajouter une page web basique index.html :
  • 26. © 2019-2020 – MAVEN TP2 – Projet Web avec Maven (WAR) 26 • Déployer l’application sur Tomcat, et lancer l’URL : http://localhost:18080/maven-web
  • 27. © 2019-2020 – MAVEN MAVEN Si vous avez des questions, n’hésitez pas à nous contacter : mhassini@gmail.com