...
This is a "hands on tutorial" for transitioning from Maven 1 to Maven 2 project management. It could be easily a large book, but I will try to keep things short and simple. If you do not understand something please please refer to the mentioned literature or feel free to ask me.
...
The reader is assumed to be completely familiar with Maven 1, and not so much with Maven 2. I recommend reading at least the User documentation http://maven.apache.org/.
I found book: Better Builds with Maven by Vincent Massol et al. (there is an electronic version available), very valuable. Published by DevZuz Library Press, April 2007.
In this hands on approach, I will convert one of the GLAST projects: org-glast-groupmanager, that was built with Maven 1, into a Maven 2 project. I will also assume that NetBeans 6.0 is used for building the project using Netbeans Maven 2 plugin. Initially I would test transitions using command line program mvn.
Install Maven 2 for your OS and set MVN_HOME, as well as create .m2 directory in your home(working) directory. Link is: http://maven.apache.org/
In Appendix One, I list typical GLAST Maven 1 project files and give a log of a successful 'maven' (Maven 1.x) run.
I will concentrate bellow only on the significant changes when transitioning from Maven 1 to Maven 2. The changes will be presented in tabular forms bellow.
...
The default directory structure is actually defined in the Maven 2 "Super POM" file, which all Maven 2 POM objects objects inherit from. Here is the GLAST directory structure for Maven 2 projects:
Code Block |
---|
artifactId/ project directory |-- .cvsignore contains files that are local to build: target, profiles.xml, etc |-- pom.xml POM file |-- profiles.xml local, user and project dependent profile definitions; this should not be version controlled |-- LICENSE.txt license of the project |-- README.txt welcome to the reader |-- src/ original src material; this should be version controlled | |-- main/ the original material for the artifact | | |-- java/ root of main Java source; wilwill be compiled into target/classes | | |-- resources/ main Java resources; will be copied into target/classes | | |-- webapp/ | | |-- WEB-INF/ | | | `-- web.xml | | `-- index.jsp web application with standard web application structure | | |-- assembly/ | | `-- dep.xml assembly descriptor for maven-assembly-plugin | | |-- filters/ resource filter properties files for main Java resources | | |-- config/ configuration files for the artifact | | |-- bash/ | | |-- groovy/ | | |-- prolog/ | | |-- sql/ sources in other technologies for the artifact | | -- site/ project documentation in different formats; mvn site will produce a project website in target/site based on this material and structure (Doxia) | | |-- apt/ | | | |-- format.apt | | | `-- index.apt documentation in APT format (wiki-like HTML generation) | | |-- fml/ | | | `-- faq.fml documentation in FML format (XML based FAQ format) | | |-- resources/ | | | |-- css/ | | | |-- img/ | | | `-- js/ site resources; will be copied into target/site as-is | | |-- site.xml site descriptor: description of site structure; this will generate menus | | `-- xdoc/ | | `-- xdoc.xml documentation in Xdoc format (XML based HTML generation; maven 1 legacy) | `-- test/ original material to test the artifact | |-- java/ root of Java source for testing the artifact; usually JUnit test classes; will be compiled into target/test-classes | |-- resources/ resources for testing the artifact; will be copied into target/test-classes | |-- filters/ r resource filter properties files for resources for testing the artifact | |-- perl/ | |-- haskell/ | |-- python/ sources in other technologies for testing the artifact `-- target/ generated material; this should not be under version control |-- artifactId-version.jar generated artifact |-- classes/ result of compilation of src/main/java and copy of src/main/resources |-- exported-pom.xml consolidated POM |-- javadoc/ javadoc of src/main/java |-- site/ project site generated by mvn site |-- surefire-reports/ test reports |-- test-classes/ result of compilation of src/test/java and copy of src/test/resources `-- announcement/ `-- announcement.vm org.codehaus.mojo:changelog-maven-plugin generates announcement mail here |
...
Note: The simplest thing to do in transition from Maven 1 to Maven 2 project building is to convert rename project.xml file into pom.xml file first. However, there are slight differences that need to be addressed and they are presented in the table bellow. I recommend creating subdirectories in the project directory called maven1 and maven2, where project files from both versions are kept (under the version control) , until you are comfortable that your project is fully converted to a Maven 2 project.
You can start by moving project.xml to pom.xml, change pomVersion to modelVersion in the table bellow and than follow some of the mappings (bold letters denote elements that need to be changed. The following table resulted from conversion of project.xml in pom.xml of the org-glast-groupmanager project. Mappings between project.xml and pom.xml project elements:
Maven 1.x | Maven 2.x | Description |
---|---|---|
<project> | <project xmlns="http://maven.apache.org/POM/4.0.0" | Keep more extended version of project, so you can visit the schema and validate document |
<pomVersion>3.0.0</pomVersion> | <modelVersion>4.0.0</modelVersion> | Differentiates between different versions of POM objects |
<groupId> | <groupId> | Stays the same |
<artifactId> | <artifactId> | Stays the same |
<currentVersion> | <version> | Same meaning |
<shortDescription> | <description> | Same meaning |
<package> | Not used, JavaDoc is done differently in Maven 2 | The Java package name of the project. This value is used when generating JavaDoc. |
<siteAddress> | Not used, hostname is part of url the corresponding <url> element | The hostname of the web server that hosts the project's web site. This is used when the web site is deployed. |
<siteDirectory> | Not used, directory part of the corresponding <url> element | The directory on the web server where the public web site for this project resides |
<repository> | Should be nested inside <repositories> element | Specifies various Maven and SCM repositories |
<developerConnection> | Should be moved to <scm> element | Defines connection to SCM (Source Control Management) for developers. |
<reports> | <reports> (and <report>*) Delete, it is deprecated in Maven 2. | This element includes the specification of reports to be included in a Maven-generated site. |
<properties> | If used inside <dependency> delete the element and use <scope>, <version>,<type> as described in http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html | Dependency properties |
<url> | If used inside <dependency>, delete it. | Different URLs nested inside other elements |
<sourceDirectory> | <sourceDirectory> | Used inside <build>, no need to specify, if the default src directory is used. |
|
|
|
|
|
|
|
|
|
3. File after substitutions:
...
Code Block |
---|
<?xml version="1.0" encoding="UTF-8"?> <!-- Maven 2 is shortly identified as maven. If we want to give a reference to Maven 1.0.2 we will call Maven 1. This POM extends SuperPOM, which means that the following elements are already defined: maven central repository (http://repo1.maven.org/maven2) maven plugin repository (http://repo1.maven.org/maven2) default build directories: target target/classes target/test-classes target/site src/main/java src/test/java src/main/scripts src/main/resources src/test/resources as well as default profiles: release-profile and default plugins: maven-source-plugin maven-javadoc-plugin maven-deploy-plugin --> <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> <artifactId>org-glast-groupmanager</artifactId> <groupId>glast</groupId> <version>1.6-SNAPSHOT</version> <description>Glast Group Manager</description> <url>http://glast-ground.slac.stanford.edu/GroupManager/</url> <name>Glast Group Manager</name> <inceptionYear>2005</inceptionYear> <repositories> <!-- Not yet defined --> </repositories> <scm> <developerConnection> scm:cvs:ext:${maven.username}@glast java.slac.stanford.edu:/cvs/java:${pom.artifactId} <!-- to be verified --> </developerConnection> </scm> <dependencies> <!-- dependencies here --> <!-- Needed by cruisecontrol --> <dependency> maven maven-scm-plugin 1.5 plugin <dependency> <groupId>tomcat</groupId> <artifactId>servlet-api</artifactId> <version>5.0.18</version> </dependency> </dependencies> </project> |
4. Profiles.xml file
Todo
3. Maven 2 repositories.
Todo
4. Web application deployments on tomcat servers
Todo
5. Using inheritance for POM objects
Todo
Appendix A
project.xml
<!-- ... -->
<!-- Needed by cruisecontrol -->
<dependency>
<groupId>maven</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.5</version>
<type>plugin</type>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resource</directory> <!-- should have been resources -->
<includes>
<include>META-INF/*.tld</include>
</includes>
</resource>
</resources>
</build>
</project>
|
4. Profiles.xml file
Todo
3. Maven 2 repositories.
Todo
4. Web application deployments on tomcat servers
Todo
5. Using inheritance for POM objects
Todo
Appendix A
Here we give Maven 1.x files for reference:
project.xml file:
Code Block |
---|
<?xml version="1.0" encoding="UTF-8"?>
<project>
<pomVersion>3</pomVersion>
<artifactId>org-glast-groupmanager</artifactId>
<groupId>glast</groupId>
<currentVersion>1.6-SNAPSHOT</currentVersion>
<shortDescription>Glast Group Manager</shortDescription>
<url>http://glast-ground.slac.stanford.edu/GroupManager/</url>
<name>Glast Group Manager</name>
<package>org.glast.groupmanager</package>
<inceptionYear>2005</inceptionYear>
<siteAddress>glast-java.slac.stanford.edu</siteAddress>
<siteDirectory>/nfs/slac/g/glast/ground/docs/${pom.artifactId}</siteDirectory>
<reports>
<report>maven-jdepend-plugin</report>
<report>maven-changes-plugin</report>
<report>maven-changelog-plugin</report>
<report>maven-developer-activity-plugin</report>
<report>maven-file-activity-plugin</report>
<report>maven-javadoc-plugin</report>
<report>maven-jxr-plugin</report>
<report>maven-junit-report-plugin</report>
<report>maven-linkcheck-plugin</report>
<report>maven-tasklist-plugin</report>
<report>taglib</report>
</reports>
<repository>
<developerConnection>
scm:cvs:ext:${maven.username}@glast-java.slac.stanford.edu:/cvs/java:${pom.artifactId}
</developerConnection>
<url>http://www-glast.stanford.edu/cgi-bin/viewcvs/${pom.artifactId}/?root=java</url>
</repository>
<dependencies>
<dependency>
<groupId>tomcat</groupId>
|
Code Block |
3 org-glast-groupmanager glast 1.6-SNAPSHOT Glast Group Manager http://glast-ground.slac.stanford.edu/GroupManager/ Glast Group Manager org.glast.groupmanager 2005 glast-java.slac.stanford.edu /nfs/slac/g/glast/ground/docs/${pom.artifactId} maven-jdepend-plugin maven-changes-plugin maven-changelog-plugin maven-developer-activity-plugin maven-file-activity-plugin maven-javadoc-plugin maven-jxr-plugin maven-junit-report-plugin maven-linkcheck-plugin maven-tasklist-plugin taglib scm:cvs:ext:${maven.username}@glast-java.slac.stanford.edu:/cvs/java:$ {pom.artifactId} http://www-glast.stanford.edu/cgi-bin/viewcvs/${pom.artifactId}/?root=java</url> </repository> <dependencies> <dependency> <groupId>tomcat</groupId> <artifactId>servlet-api</artifactId> <version>5.0.18</version> </dependency> <!- Needed by cruisecontrol -> <dependency> <groupId>maven</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.5</version> <type>plugin</type> <url>http://www.ibiblio.org/maven/maven/plugins/</url </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resource</directory> <includes> <include>META<artifactId>servlet-INF/*.tld</include>api</artifactId> <version>5.0.18</version> </includes> dependency> <!-- more dependencies --> </resource> <!- Needed by cruisecontrol -> </resources><dependency> </build> </project> |
project.properties file:
Code Block |
---|
maven.repo.remote=http://mirrors <groupId>maven</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.5</version> <type>plugin</type> <url>http://www.ibiblio.org/pub/mirrorsmaven/maven,http:/plugins/java.freehep.org/maven,http://glast-ground.slac.stanford.edu/maven maven.war.src=src/webapp maven.tomcat.war.context=/GroupManager maven.war.final.name=GroupManager.war maven.tomcat.precompile=false maven.repo.list=glast maven.repo.glast=scpexe://glast-java.slac.stanford.edu maven.repo.glast.directory=/nfs/slac/g/glast/ground/maven maven.site.deploy.method=ssh maven.compile.target=1.5 maven.compile.source=1.5 taglib.src.dir=src/main/resource/META-INF |
maven.xml file:
</url
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>META-INF/*.tld</include>
</includes>
</resource>
</resources>
</build>
</project>
|
project.properties file:
Code Block |
---|
maven.repo.remote=http://mirrors.ibiblio.org/pub/mirrors/maven,http://java.freehep.org/maven,http://glast-ground.slac.stanford.edu/maven
maven.war.src=src/webapp
maven.tomcat.war.context=/GroupManager
maven.war.final.name=GroupManager.war
maven.tomcat.precompile=false
maven.repo.list=glast
maven.repo.glast=scpexe://glast-java.slac.stanford.edu
maven.repo.glast.directory=/nfs/slac/g/glast/ground/maven
maven.site.deploy.method=ssh
maven.compile.target=1.5
maven.compile.source=1.5
taglib.src.dir=src/main/resource/META-INF
|
maven.xml file:
Code Block |
---|
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:j="jelly:core"
xmlns:u="jelly:util" default="war">
<goal name="webInstall">
<attainGoal name="tomcat:install"/>
</goal>
<goal name="webReload">
<attainGoal name="tomcat:remove"/>
<attainGoal name="tomcat:install"/>
</goal>
<goal name="webRemove">
<attainGoal name="tomcat:remove"/>
</goal>
<goal name="tomcat03Redeploy">
<u:properties file="${user.home}/tomcat03.properties" />
<attainGoal name="clean"/>
<attainGoal name="tomcat:remove"/>
<attainGoal name="tomcat:deploy"/>
</goal>
<goal name="tomcat03Deploy">
<u:properties file="${user.home}/tomcat03.properties" /> |
Code Block |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:j="jelly:core" <attainGoal xmlns:uname="jelly:util" default="war"> clean"/> <attainGoal name="tomcat:deploy"/> </goal> <goal name="webInstalltomcat01Redeploy"> <attainGoal name="tomcat:install"<u:properties file="${user.home}/tomcat01.properties" /> </goal> <goal<attainGoal name="webReloadclean"/> <attainGoal name="tomcat:remove"/> <attainGoal name="tomcat:installdeploy"/> </goal> <goal name="webRemovetomcat01Deploy"> <attainGoal<u:properties namefile="tomcat:remove"${user.home}/tomcat01.properties" /> </goal> <goal<attainGoal name="tomcat03Redeployclean"/> <u:properties<attainGoal filename="${user.home}/tomcat03.properties" tomcat:deploy"/> </goal> </project> |
build.properties file:
Code Block |
---|
maven.netbeans.exec.run=webReload maven.netbeans.exec.build=war:war tomcat01.properties: maven.repo.remote =http://mirrors.ibiblio.org/pub/mirrors/maven,http://java.freehep.org/maven,http://glast-ground.slac.stanford.edu/maven,http://www.codeczar.com/maven maven.tomcat.host=glast-tomcat01.slac.stanford.edu maven.tomcat.port=8080 maven.tomcat.username=glast maven.tomcat.password=... maven.tomcat.precompile=false maven.compile.compilerargs=-Xlint:unchecked maven.tomcat.war.context=/dp2 maven.war.src=src/main/webapp maven.war.final.name=dp2.war =/dp2 maven.war.src=src/main/webapp maven.war.final.name=dp2.war |
Code Block |
---|
__ __
| \/ |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\__,_|\_/\___|_||_| v. 1.0.2 |
Code Block |
build:start: war:init: war:war-resources: mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF copy Copying 15 files to /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager copy Copying 1 file to /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF java:prepare-filesystem: mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/classes java:compile: echo Compiling to /Users/igor/work/java/glast/org-glast-groupmanager/target/classes javac Compiling 6 source files to /Users/igor/work/java/glast/org-glast-groupmanager/target/classes java:jar-resources: Copying 1 file to /Users/igor/work/java/glast/org-glast-groupmanager/target/classes test:prepare-filesystem: mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/test-classes mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/test-reports test:test-resources: test:compile: echo No test source files to compile. test:test: echo No tests to run. war:webapp: echo Assembling webapp org-glast-groupmanager mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF/lib mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF/tld mkdir Created dir: /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF/classes copy Copying 1 file to /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF/lib ... copy Copying 11 files to /Users/igor/work/java/glast/org-glast-groupmanager/target/org-glast-groupmanager/WEB-INF/classes war:war: echo Building WAR org-glast-groupmanager jar Building jar: /Users/igor/work/java/glast/org-glast-groupmanager/target/GroupManager.war BUILD SUCCESSFUL Total time: 4 seconds Finished at: Thu Nov 29 15:59:57 PST 2007 |