This is an old revision of the document!
TP http://dcabasson.developpez.com/articles/java/maven/introduction-maven2/
9. Création d'un nouveau projet avec le plug-in archetype
[disi@cas4 ~]$ mvn archetype:create -DgroupId=com.javaworld.hotels -DartifactId=HotelDatabase -Dpackagename=com.javaworld.hotels [INFO] Scanning for projects... Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom (5 KB at 8.9 KB/sec) ... Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.2/maven-archetype-plugin-2.2.jar (86 KB at 843.0 KB/sec) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-archetype-plugin:2.2:create (default-cli) @ standalone-pom --- Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/2.2/archetype-catalog-2.2.pom ... Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.jar (5394 KB at 5749.5 KB/sec) [WARNING] This goal is deprecated. Please use mvn archetype:generate instead [INFO] Defaulting package to group ID: com.javaworld.hotels Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/maven-metadata.xml Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/maven-metadata.xml (531 B at 14.8 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar (7 KB at 137.7 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom (2 KB at 59.5 KB/sec) [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.javaworld.hotels [INFO] Parameter: packageName, Value: com.javaworld.hotels [INFO] Parameter: package, Value: com.javaworld.hotels [INFO] Parameter: artifactId, Value: HotelDatabase [INFO] Parameter: basedir, Value: /home/disi [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: /home/disi/HotelDatabase [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.536s [INFO] Finished at: Tue Dec 16 17:15:08 CET 2014 [INFO] Final Memory: 11M/27M [INFO] ------------------------------------------------------------------------
Vous avez maintenant une structure de projet Maven 2 toute neuve. Allez dans le répertoire HotelDatabase pour continuer ce tutoriel.
[disi@cas4 ~]$ ls HotelDatabase [disi@cas4 ~]$ cd HotelDatabase/ [disi@cas4 HotelDatabase]$ ls pom.xml src
recopie des sources
[disi@cas4 HotelDatabase]$ cp /tmp/application/HotelDatabase/src/main/java/com/javaworld/hotels/model/HotelModel.java src/main/java/com/javaworld/hotels/ [disi@cas4 HotelDatabase]$ cp -a /tmp/application/HotelDatabase/src/main/java/com/javaworld/hotels/businessobjects src/main/java/com/javaworld/hotels/ [disi@cas4 HotelDatabase]$ cp -a /tmp/application/HotelDatabase/src/main/java/com/javaworld/hotels/dao src/main/java/com/javaworld/hotels/ [disi@cas4 HotelDatabase]$ cp -a /tmp/application/HotelDatabase/src/main/java/com/javaworld/hotels/model src/main/java/com/javaworld/hotels/ [disi@cas4 src]$ rm -rf test [disi@cas4 src]$ cp -a /tmp/application/HotelDatabase/src/test . [disi@cas4 src]$ ls main test [disi@cas4 src]$ ls -l test/java/com/javaworld/hotels/ total 0 drwxr-xr-x 2 disi disi 32 9 févr. 2006 model [disi@cas4 src]$ ls -l test/java/com/javaworld/hotels/model/ total 4 -rw-r--r-- 1 disi disi 1605 9 févr. 2006 HotelModelTest.java [disi@cas4 HotelDatabase]$ cp /tmp/application/HotelDatabase/pom.xml .
test unitaires
[disi@cas4 HotelDatabase]$ mvn test [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.javaworld.hotels:HotelDatabase:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 17 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Hotel Database tutorial application 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ HotelDatabase --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/disi/HotelDatabase/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ HotelDatabase --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ HotelDatabase --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ HotelDatabase --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /home/disi/HotelDatabase/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ HotelDatabase --- [INFO] Surefire report directory: /home/disi/HotelDatabase/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javaworld.hotels.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec Running com.javaworld.hotels.model.HotelModelTest Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Results : Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.012s [INFO] Finished at: Tue Dec 16 17:53:55 CET 2014 [INFO] Final Memory: 8M/21M [INFO] ------------------------------------------------------------------------ or [disi@cas4 HotelDatabase]$ mvn test -Dtest=HotelModelTest ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javaworld.hotels.model.HotelModelTest Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 sec Results : Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.345s ...
install
[disi@cas4 HotelDatabase]$ mvn install [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.javaworld.hotels:HotelDatabase:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 17 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Hotel Database tutorial application 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ HotelDatabase --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/disi/HotelDatabase/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ HotelDatabase --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ HotelDatabase --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ HotelDatabase --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ HotelDatabase --- [INFO] Surefire report directory: /home/disi/HotelDatabase/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javaworld.hotels.model.HotelModelTest Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 sec Results : Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ HotelDatabase --- [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ HotelDatabase --- [INFO] Installing /home/disi/HotelDatabase/target/HotelDatabase.jar to /home/disi/.m2/repository/com/javaworld/hotels/HotelDatabase/1.0-SNAPSHOT/HotelDatabase-1.0-SNAPSHOT.jar [INFO] Installing /home/disi/HotelDatabase/pom.xml to /home/disi/.m2/repository/com/javaworld/hotels/HotelDatabase/1.0-SNAPSHOT/HotelDatabase-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.624s [INFO] Finished at: Sat Dec 20 22:31:05 CET 2014 [INFO] Final Memory: 7M/16M [INFO] ------------------------------------------------------------------------
Creation de la WebApp sous form d'une simple page JSP qui appelra notre class jar ci-dessus
[disi@cas4 ~]$ mvn archetype:create -DgroupId=com.javaworld.hotels -DartifactId=HotelWebapp -Dpackagename=com.javaworld.hotels -DarchetypeArtifactId=maven-archetype-webapp [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-archetype-plugin:2.2:create (default-cli) @ standalone-pom --- [WARNING] This goal is deprecated. Please use mvn archetype:generate instead [INFO] Defaulting package to group ID: com.javaworld.hotels [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.javaworld.hotels [INFO] Parameter: packageName, Value: com.javaworld.hotels [INFO] Parameter: package, Value: com.javaworld.hotels [INFO] Parameter: artifactId, Value: HotelWebapp [INFO] Parameter: basedir, Value: /home/disi [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: /home/disi/HotelWebapp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.723s [INFO] Finished at: Sat Dec 20 22:37:44 CET 2014 [INFO] Final Memory: 9M/21M [INFO] ------------------------------------------------------------------------ [disi@cas4 ~]$ cd HotelWebapp/ [disi@cas4 HotelWebapp]$ [disi@cas4 HotelWebapp]$ ls pom.xml src
recopie du jsp depuis les sources (à la place du default “hello world” )
[disi@cas4 HotelWebapp]$ cp /tmp/application/HotelWebapp/src/main/webapp/index.jsp src/main/webapp/index.jsp
ajout de la dependance dans ce projet WebApp sur le jar de l'etape precedente
[disi@cas4 HotelWebapp]$ diff -ur pom.xml pom.xml.orig --- pom.xml 2014-12-20 22:43:53.000000000 +0100 +++ pom.xml.orig 2014-12-20 22:45:34.657721908 +0100 @@ -14,11 +14,6 @@ <version>3.8.1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>com.javaworld.hotels</groupId> - <artifactId>HotelDatabase</artifactId> - <version>1.0-SNAPSHOT</version> - </dependency> </dependencies> <build> <finalName>HotelWebapp</finalName>
install
[disi@cas4 HotelWebapp]$ mvn install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building HotelWebapp Maven Webapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom (7 KB at 26.3 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.jar (76 KB at 382.0 KB/sec) [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ HotelWebapp --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ HotelWebapp --- [INFO] No sources to compile [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ HotelWebapp --- [debug] execute contextualize [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/disi/HotelWebapp/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ HotelWebapp --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ HotelWebapp --- [INFO] No tests to run. [INFO] Surefire report directory: /home/disi/HotelWebapp/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ HotelWebapp --- Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.pom (4 KB at 75.7 KB/sec) ... Downloaded: http://repo.maven.apache.org/maven2/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar (422 KB at 1094.3 KB/sec) [INFO] Packaging webapp [INFO] Assembling webapp [HotelWebapp] in [/home/disi/HotelWebapp/target/HotelWebapp] [INFO] Processing war project [INFO] Copying webapp resources [/home/disi/HotelWebapp/src/main/webapp] [INFO] Webapp assembled in [81 msecs] [INFO] Building war: /home/disi/HotelWebapp/target/HotelWebapp.war [INFO] WEB-INF/web.xml already added, skipping [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ HotelWebapp --- [INFO] Installing /home/disi/HotelWebapp/target/HotelWebapp.war to /home/disi/.m2/repository/com/javaworld/hotels/HotelWebapp/1.0-SNAPSHOT/HotelWebapp-1.0-SNAPSHOT.war [INFO] Installing /home/disi/HotelWebapp/pom.xml to /home/disi/.m2/repository/com/javaworld/hotels/HotelWebapp/1.0-SNAPSHOT/HotelWebapp-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.688s [INFO] Finished at: Sat Dec 20 22:44:02 CET 2014 [INFO] Final Memory: 8M/20M [INFO] ------------------------------------------------------------------------