Construire un projet de modules multiples avec Maven
1. Introduction
Ce document est basé sur:
Eclipse 4.6 (NEON)
Vous voyez l'outil Maven avancé. Si vous êtes un débutant Maven. Vous devriez regarder la documentation pour les débutants Maven (Maven Hello world) à:
2. L'exemple de modèle
Celui- ci est un modèle de l'exemple dans ce document.

Les objectifs des instructions sont:
- Comment un module utilisant un autre module à Maven
- Emballage un module multiple en utilisant Maven (sortie: jar, war)
MathWebApp: est un projet de l'application web
MathLibrary: est un Project bibliothèque, qui comprend des classes utilitaires utilisées par MathWebApp.
MavenParent: est un projet qui va emballer 2 projets ci- dessus, c'est une module principale, et 2 projets ci- dessus sont considérés comme ses 2 submodules. MavenParent va:
MathLibrary: est un Project bibliothèque, qui comprend des classes utilitaires utilisées par MathWebApp.
MavenParent: est un projet qui va emballer 2 projets ci- dessus, c'est une module principale, et 2 projets ci- dessus sont considérés comme ses 2 submodules. MavenParent va:
- Emballer MathLibary en le fichcier jar
- Emballer MathWebApp en le fichier war.
3. Créer le projet MathLibrary
- File/New/Other...

C'est un projet simple, donc nous n'avons pas besoin de sélectionner un archétype (archetype) Maven.
Sélectionnez:
Sélectionnez:
- Create a simple project (skip archetype selection)

Saisissez:
- Group Id: org.o7planning
- Artifact Id: MathLibrary
- Packaging: jar
Ignorer les informations du module principale.

Le projet est créé:

Créer une nouvelle classs MathUtils:

MathUtils.java
package org.o7planning.mathutils;
public class MathUtils {
public static int sum(int a, int b) {
return a + b;
}
}
4. Créer le projet MathWebApp
- File/New/Other..


Sélectionnez l'archétype (archetype) maven-archetype-webapp. Eclipse créera un Project Maven dont la structure est sous forme d'une application web.

Saisissez:
- Group Id: org.o7planning
- Artifact Id: MathWebApp
- Version: 0.0.2-SNAPSHOT
- Package: org.o7planning.mathwebapp

Voici une capture d'écran, le projet MathWebApp a été créé. Vous pouvez voir un message d'erreur quelque part sur le projet, ne vous inquiétez pas, parce que vous n'avez pas déclaré la bibliothèque Servlet.

Eclipse crée ce projet dont la structure peut être fautive. Vous devez la vérifier.

- Open file pom.xml

Ajoutez:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
Comme l'illustration suivante:

Maintenant il n'y a plus d'erreur:

Continuez de configurer maven, MathWebApp et utilisez MathLibrary:
<dependency>
<groupId>org.o7planning</groupId>
<artifactId>MathLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
MathWebApp/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>MathWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.2-SNAPSHOT</version>
<name>MathWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.o7planning</groupId>
<artifactId>MathLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>MathWebApp</finalName>
</build>
</project>
Éditez le fichier index.jsp en utilisant la classe utilitaire MathUtils dans le projet MathLibrary.
index.jsp
<html>
<body>
<h2>Hello World!</h2>
<%
int a = 100;
int b = 200;
int c = org.o7planning.mathutils.MathUtils.sum(a,b);
out.println("<h2>"+ c+"</h2>");
%>
</body>
</html>
5. Créer le projet MavenParent
Créer un projet Java commun.
- File/New/Other..



Cliquez sur le bouton droit du project MavenParent qui vient d'être créé, et le convertissez (convert) en Maven Project.

Saisissez:
- Group Id: org.o7planning
- Artifact Id: MavenParent
- Version: 1.0.0-SNAPSHOT
- Packaging: pom


6. Configurer Maven la relation entre les projets
Cette image décrit brièvement la configuration relationnelle Maven parmi les module (Project).

Ouvrez le fichier pom.xml de 2 projets MathLibrary & MathWebApp et ajoutez:
<parent>
<groupId>org.o7planning</groupId>
<artifactId>MavenParent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../MavenParent/pom.xml</relativePath>
</parent>
Ouvrez le fichier pom.xml de MavenParent et ajoutez:
<modules>
<module>../MathLibrary</module>
<module>../MathWebApp</module>
</modules>
MathLibrary/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>MathLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.o7planning</groupId>
<artifactId>MavenParent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../MavenParent/pom.xml</relativePath>
</parent>
</project>
MathWebApp/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>MathWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.2-SNAPSHOT</version>
<name>MathWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.o7planning</groupId>
<artifactId>MathLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<parent>
<groupId>org.o7planning</groupId>
<artifactId>MavenParent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../MavenParent/pom.xml</relativePath>
</parent>
<build>
<finalName>MathWebApp</finalName>
</build>
</project>
MavenParent/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>MavenParent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../MathLibrary</module>
<module>../MathWebApp</module>
</modules>
</project>
Tutoriels Maven
- Installer Maven pour Eclipse
- Le Tutoriel de Maven pour débutant
- Maven Gérer les dépendances
- Construire un projet de modules multiples avec Maven
- Exécutez l'application Web Java Maven dans Tomcat Maven Plugin
- Exécutez l'application Web Java Maven dans Jetty Maven Plugin
- Installer Tycho pour Eclipse
- Créer un projet Java OSGi avec Maven et Tycho
- Créer un projet Maven Web App vide dans Eclipse
- Intégration OSGi et AspectJ
Show More