Maven archetype, qu’est ce que c’est ?#

Comme indiqué sur le site officiel, Maven archetype est une boîte à outils de modélisation qui vous permettra de créer vos projets à partir de template préexistant ou de créer vos propres templates paramétrés et faciliter vos créations de nouveaux projets

En quelques secondes, un développeur peut disposer d’un projet Maven fonctionnel simplement en fournissant le groupId, l’artifactId et la version de l’archetype qu’il veut utiliser pour démarrer son projet.

1$ mvn archetype:generate -DarchetypeGroupId=<groupId>
2                        -DarchetypeArtifactId=<artifactId>
3                        -DarchetypeVersion=<version>

Ce n’est pas plus compliqué que ça pour démarrer un projet depuis un archetype existant.

L’inconvénient, c’est qu’à l’heure où j’écris ces lignes, il existe plus de 2760 archetypes publiés sur maven central:

 1$ mvn archetype:generate 
 2[INFO] Scanning for projects...
 3[INFO]
 4[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
 5[INFO] Building Maven Stub Project (No POM) 1
 6[INFO] --------------------------------[ pom ]---------------------------------
 7[INFO]
 8[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
 9[INFO]
10[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
11[INFO]
12[INFO]
13[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
14[INFO] Generating project in Interactive mode
15[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
16Choose archetype:
17...
182756: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
192757: remote -> ws.osiris:osiris-archetype (Maven Archetype for Osiris)
202758: remote -> xyz.luan.generator:xyz-gae-generator (-)
212759: remote -> xyz.luan.generator:xyz-generator (-)
222760: remote -> za.co.absa.hyperdrive:component-archetype (-)
23Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 

Difficile de retrouver ses jeunes avec toutes ces solutions. Heureusement, il existe la possibilité de filtrer les résultats comme ceci :

1$ mvn archetype:generate -Dfilter=<groupId>:<artefactId>

Créons par exemple une simple application Spring Boot en recherchant les archetypes qui sont sous le groupId org.springframework et qui contiennent spring-boot dans leur arteficatId:

 1$ mvn archetype:generate -Dfilter=org.springframework:spring-boot
 2[INFO] Scanning for projects...
 3[INFO]
 4[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
 5[INFO] Building Maven Stub Project (No POM) 1
 6[INFO] --------------------------------[ pom ]---------------------------------
 7[INFO]
 8[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
 9[INFO]
10[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
11[INFO]
12[INFO]
13[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
14[INFO] Generating project in Interactive mode
15[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
16Choose archetype:
171: remote -> org.springframework.boot:spring-boot-sample-actuator-archetype (Spring Boot Actuator Sample)
18...
1917: remote -> org.springframework.boot:spring-boot-sample-simple-archetype (spring-boot-sample-simple-archetype)
20...
2126: remote -> org.springframework.boot:spring-boot-sample-xml-archetype (Spring Boot XML Sample)
22Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 17
23Choose org.springframework.boot:spring-boot-sample-simple-archetype version:
241: 1.0.2.RELEASE
252: 1.0.2.BUILD-SNAPSHOT
26Choose a number: 2: 1
27Downloading from nexus: http://nexus.ucm.be/nexus/repository/maven-sec/org/springframework/boot/spring-boot-sample-simple-archetype/1.0.2.RELEASE/spring-boot-sample-simple-archetype-1.0.2.RELEASE.pom
28Downloaded from nexus: http://nexus.ucm.be/nexus/repository/maven-sec/org/springframework/boot/spring-boot-sample-simple-archetype/1.0.2.RELEASE/spring-boot-sample-simple-archetype-1.0.2.RELEASE.pom (2.2 kB at 8.3 kB/s)
29Downloading from nexus: http://nexus.ucm.be/nexus/repository/maven-sec/org/springframework/boot/spring-boot-sample-simple-archetype/1.0.2.RELEASE/spring-boot-sample-simple-archetype-1.0.2.RELEASE.jar
30Downloaded from nexus: http://nexus.ucm.be/nexus/repository/maven-sec/org/springframework/boot/spring-boot-sample-simple-archetype/1.0.2.RELEASE/spring-boot-sample-simple-archetype-1.0.2.RELEASE.jar (6.3 kB at 24 kB/s)
31Define value for property 'groupId': com.andycostanza
32Define value for property 'artifactId': my-old-spring-boot-app
33Define value for property 'version' 1.0-SNAPSHOT: :
34Define value for property 'package' com.andycostanza: :
35Confirm properties configuration:
36groupId: com.andycostanza
37artifactId: my-old-spring-boot-app
38version: 1.0-SNAPSHOT
39package: com.andycostanza
40 Y: :
41[INFO] ----------------------------------------------------------------------------
42[INFO] Using following parameters for creating project from Archetype: spring-boot-sample-simple-archetype:1.0.2.RELEASE
43[INFO] ----------------------------------------------------------------------------
44[INFO] Parameter: groupId, Value: com.andycostanza
45[INFO] Parameter: artifactId, Value: my-old-spring-boot-app
46[INFO] Parameter: version, Value: 1.0-SNAPSHOT
47[INFO] Parameter: package, Value: com.andycostanza
48[INFO] Parameter: packageInPathFormat, Value: com/andycostanza
49[INFO] Parameter: package, Value: com.andycostanza
50[INFO] Parameter: version, Value: 1.0-SNAPSHOT
51[INFO] Parameter: groupId, Value: com.andycostanza
52[INFO] Parameter: artifactId, Value: my-old-spring-boot-app
53[INFO] Project created from Archetype in dir: /home/andy/projects/my-old-spring-boot-app
54[INFO] ------------------------------------------------------------------------
55[INFO] BUILD SUCCESS
56[INFO] ------------------------------------------------------------------------

Nous venons de créer une très ancienne application Spring Boot (version 1.0.2 de 2014) que je vous déconseille d’utiliser car nous sommes passé à la version 2.2.x maintenant. Je pense que Spring a abandonné ces archetypes au détriment de leur site Spring initializr

Pourquoi créer son Maven archetype#

Si comme moi, vous passez par Spring initializr pour créer vos projets mais que vous devez ensuite ajouter du “boilerplate” dans vos pom.xml, ajouter le fichier de config du CI, rajouter des fichiers properties de configuration, ou que sais-je encore.

Grâce au Maven archetype, nous allons standardiser le développement d’applications en donnant à nos développeurs des templates prêt à l’emploi, déployés dans notre repository maven d’entreprise, en réduisant au strict minimum les tâches de configurations nécessaires au déploiement de celles-ci.

Comment mettre en place un Maven archetype#

1. Créer son archetype en utilsant l’archetype de création d’archetype#

Drôle de titre de section, mais c’est pourtant le cas. Maven a mis à notre disposition une dizaine d’archetypes pour mettre en place nos projets. L’un de ceux-ci permet la création d’archetype maven:

1$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes
2                        -DarchetypeArtifactId=maven-archetype-archetype
3                        -DarchetypeVersion=1.4

Créons maintenant notre premier archetype qui sera utilisé comme référence pour nos futures applications Spring Boot 2.x

 1[INFO] Scanning for projects...
 2[INFO]
 3[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
 4[INFO] Building Maven Stub Project (No POM) 1
 5[INFO] --------------------------------[ pom ]---------------------------------
 6[INFO]
 7[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
 8[INFO]
 9[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
10[INFO]
11[INFO]
12[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
13[INFO] Generating project in Interactive mode
14[INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-archetype:1.0-alpha-4] found in catalog remote
15Define value for property 'groupId': com.andycostanza.archetypes
16Define value for property 'artifactId': my-spring-boot-archetype
17Define value for property 'version' 1.0-SNAPSHOT: :
18Define value for property 'package' com.andycostanza.archetypes: :
19Confirm properties configuration:
20groupId: com.andycostanza.archetypes
21artifactId: my-spring-boot-archetype
22version: 1.0-SNAPSHOT
23package: com.andycostanza.archetypes
24 Y: :
25[INFO] ----------------------------------------------------------------------------
26[INFO] Using following parameters for creating project from Archetype: maven-archetype-archetype:1.4
27[INFO] ----------------------------------------------------------------------------
28[INFO] Parameter: groupId, Value: com.andycostanza.archetypes
29[INFO] Parameter: artifactId, Value: my-spring-boot-archetype
30[INFO] Parameter: version, Value: 1.0-SNAPSHOT
31[INFO] Parameter: package, Value: com.andycostanza.archetypes
32[INFO] Parameter: packageInPathFormat, Value: com/andycostanza/archetypes
33[INFO] Parameter: package, Value: com.andycostanza.archetypes
34[INFO] Parameter: version, Value: 1.0-SNAPSHOT
35[INFO] Parameter: groupId, Value: com.andycostanza.archetypes
36[INFO] Parameter: artifactId, Value: my-spring-boot-archetype
37[INFO] Project created from Archetype in dir: /home/andy/projects/my-spring-boot-archetype
38[INFO] ------------------------------------------------------------------------
39[INFO] BUILD SUCCESS
40[INFO] ------------------------------------------------------------------------