Building and Managing Java Projects With Maven: Alan Wang Connectria
Building and Managing Java Projects With Maven: Alan Wang Connectria
Building and Managing Java Projects With Maven: Alan Wang Connectria
Agenda
What is Maven?
A J2EE example
Customizing and extending Maven
Tips and discussions
What is Maven?
makefile
target
Ant
build.xml
target
Maven
project.xml
maven.xml
goals
site
junit
etc
test
cactus
artifact
xdoc
ear
gump
xdoclet
javadoc
java
jar
war
ejb
ant
forehead classloader util launcher
Maven
plugins
Architecture Overview
Build System
project.xml (pom)
Local
Repo
maven.xml (goals)
maven
Site
http
Remote
Repo
Artifact Repository
<dependencies>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.5.1</version>
<type>jar</type>
</dependency>
</dependencies>
- repository
- []
- xalan
- jars
- xalan-2.5.0.jar
- xalan-2.5.1.jar
- []
- []
Artifact Repository
Local Repository
A local mirror/cache of downloaded
artifacts from remote repositories.
Located at
${user.home}/.maven/repository
${mave.repo.local}/<groupId>/<type>s/<artifactId>-<version>.<type>
Implementing an Example
Get Started
Download from
http://maven.apache.org/start/download.html
Directory Layout
Project Directory Layout
sampleservice
project.xml
maven.xml
project.properties
application/
service-data/
service-ejb/
service-web/
target/
xdocs/
- EJB/WS component
- Web Application component
Directory Layout
A Component Directory Layout
service-data
- Data component subproject
project.xml
maven.xml
project.properties
src/
conf/
java/
test/
target/
xdocs/
Project Build
Source code and test code location.
Resources location
Project Dependency
Libraries needed for build and runtime.
Project Reports
Junit reports
Javadoc reports
Checkstyle reports, .etc
<description>
A detailed description about this demo
</description>
<url>http://www.myinc.com/sampleservice/</url>
<issueTrackingUrl/>
<siteAddress>dev.myinc.com</siteAddress> <!-- Used in deployment -->
<siteDirectory>/www/sampleservice/</siteDirectory> <!-- Used in deployment -->
<!-- Used in deployment. If defined, it overrides ${maven.repo.central} -->
<distributionSite>/www/dist/sampleservice</distributionSite>
<!-- Used in deployment, final distribution directory -->
<distributionDirectory>/www/www.myinc.com/somedir</distributionDirectory>
<id>1.0-beta-1</id>
<mailingLists/>
<name>1.0-beta-1</name>
<contributors/>
<tag>1.0-beta-1</tag>
<developers/>
</version>
</versions>
<branches/>
[]
<dependency>
<groupId>bcel</groupId>
<artifactId>bcel</artifactId>
<version>5.1</version>
<properties>
<classloader>root</classloader>
</properties>
</dependency>
[]
<dependency>
<groupId>weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>8.1.1</version>
<properties>
<classloader>root</classloader>
</properties>
</dependency>
project.properties
## Dependency override
maven.jar.override = on
maven.jar.weblogic = ${weblogic.home}/lib/weblogic.jar
maven.jar.webservices = ${weblogic.home}/lib/webservices.jar
<build>
<nagEmailAddress>[email protected]</nagEmailAddress>
<sourceDirectory>${src.java.dir}</sourceDirectory>
<unitTestSourceDirectory>${src.test.dir}</unitTestSourceDirectory>
src/java
<aspectSourceDirectory/>
[]
src/aspect
src/test
</document>
Property Processing
${project.home}/project.properties
Project scope
properties
${project.home}/build.properties
Properties
specific to each
build
${user.home}/build.properties
Properties
specific to each
user
CLI Dfoo=bar
Sample build.properties:
bea.home=c:/bea81sp1
oracle.home=c:/oracle/ora9i
EJBs
Web Application
Data
(jar)
EJB
(jar)
Webapp
(war)
EAR
Subproject: service-data
project.xml (POM)
<project>
<extend>../project.xml</extend>
<name>Sample Service Data
Module</name>
<id>sampleservice-data</id>
</project>
Note: POM can be inherited.
sampleservice
project.xml
maven.xml
application/
service-data/
project.xml
maven.xml
src/
service-ejb/
service-web/
xdocs/
Subproject: service-data
maven.xml (Goals)
<project default="build"
xmlns:j="jelly:core"
xmlns:maven="jelly:maven"
xmlns:ant="jelly:ant">
<goal name="build"
prereqs="jar:install"/>
</project>
sampleservice
project.xml
maven.xml
application/
service-data/
project.xml
maven.xml
src/
service-ejb/
service-web/
xdocs/
Local
Repo
Subproject: service-ejb
project.xml (POM)
<project>
<extend>../project.xml</extend>
<name>Sample Service EJB</name>
<id>sampleservice-ejb</id>
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>sampleservice-data</artifactId>
<version>${pom.currentVersion}</version>
<properties>
<ejb.manifest.classpath>true</ejb.manifest
.classpath>
</properties>
</dependency>
sampleservice
project.xml
maven.xml
application/
service-data/
service-ejb/
project.xml
maven.xml
src/
service-web/
xdocs/
</project>
xdoclet
compile
ws-gen
ejb-jar
Local
Repo
Subproject: service-ejb
Maven XDoclet Plugin
Note: Current version 1.2 does not work out-of-box (needed to fix the project.xml)
Subproject: service-ejb
Maven XDoclet Plugin - Properties
## EJBDoclet
maven.xdoclet.ejbdoclet.fileset.0.include=**/ejb/**/*Bean.java
maven.xdoclet.ejbdoclet.ejbSpec=2.0
maven.xdoclet.ejbdoclet.verbose=true
maven.xdoclet.ejbdoclet.session.0=false
maven.xdoclet.ejbdoclet.localhomeinterface.0=true
maven.xdoclet.ejbdoclet.localinterface.0=true
maven.xdoclet.ejbdoclet.utilobject.0=true
## EJBDoclet WebLogic Nested Element
maven.xdoclet.ejbdoclet.weblogic.0=true
maven.xdoclet.ejbdoclet.weblogic.0.mergeDir=${src.dir}/ejbdoclet
maven.xdoclet.ejbdoclet.weblogic.0.destDir=${maven.xdoclet.ejbdoclet.deploym
entdescriptor.0.destDir}
Subproject: service-ejb
Web Services
</goal>
project.properties
Use tag:
## Web Services
@wlws
maven.webservice.javaComponents=\
com.myinc.sampleservice.ejb.BasicAccountInquiry,\
com.myinc.sampleservice.ejb.ExpandedAccountInquiry,\
com.myinc.sampleservice.ejb.DetailedAccountInquiry
maven.webservice.autotype.package=com.myinc.sampleservice.autotype
maven.webservice.autotype.keepgenerated=false
Subproject: service-ejb
Maven EJB Plugin
sampleservice
project.xml
Currently no container specific ejb-jar
maven.xml
application/
<goal name="ejbjar" prereqs="java:compile">
<path id="ejb.classpath">
service-data/
<pathelement location="${maven.build.dest}"/>
service-ejb/
<path refid="maven.dependency.classpath"/>
project.xml
</path>
maven.xml
<j:set var="maven.ejb.descriptordir"
value="${pom.getPluginContext('maven-xdoclet-plugin')./>
src/
<ejbjar srcdir="${maven.build.dest}"
service-web/
descriptordir="${maven.ejb.descriptordir}"
xdocs/
flatdestdir="true"
basejarname="${pom.artifactId}-${pom.currentVersion}">
<classpath refid="ejb.classpath"/>
<weblogic destdir="${maven.build.dir}newCMP="true"
outputdir="${maven.build.dir}/ejb" rebuild="false"
ejbcclass="weblogic.ejbc"
</weblogic>
Subproject: service-ejb
Making the Final EJB Jar
<project default="build>
<goal name="build" prereqs="ejb:install"/>
<preGoal name="java:compile">
<attainGoal name="xdoclet:ejbdoclet"/>
</preGoal>
<postGoal name="java:compile">
<attainGoal name="wsgen"/>
<attainGoal name="java:jar-resources"/>
</postGoal>
<preGoal name="ejb:ejb">
<attainGoal name="ejbjar"/>
</preGoal>
sampleservice
project.xml
maven.xml
application/
service-data/
service-ejb/
project.xml
maven.xml
src/
service-web/
xdocs/
<postGoal name="ejb:install">
<artifact:install
artifact="${maven.build.dir}/${pom.artifactId}-${pom.currentVersion}.xml"
type="xml project="${pom}"/>
</postGoal>
Subproject: service-web
project.xml (POM)
<project>
<extend>../project.xml</extend>
<name>Sample Service Web Application</name>
<id>sampleservice-web</id>
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>sampleservice-data</artifactId>
<version>${pom.currentVersion}</version>
</dependency>
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>sampleservice-ejb</artifactId>
<version>${pom.currentVersion}</version>
<type>ejb</type>
<properties>
<web-service>true</web-service>
</properties>
</dependency>
sampleservice
project.xml
maven.xml
application/
service-data/
service-ejb/
service-web/
project.xml
maven.xml
src/
xdocs/
custom
property
Subproject: service-web
maven.xml (goals)
<project default=build>
preGoal is
used to generate
web services DD
file if defined
Subproject: application
xdocs/
Subproject: application
EAR Packaging
<project default=build>
<goal name="build" prereqs="ear"/>
<preGoal name="ear:generate-ear-descriptor">
<mkdir dir="${maven.ear.appxml.dir}"/>
</preGoal>
</project>
sampleservice
project.xml
maven.xml
application/
project.properties
project.xml
## Ear related
maven.xml
maven.ear.appxml.dir = ${maven.build.dir}/application/META-INF
src/
maven.ear.appxml = ${maven.ear.appxml.dir}/application.xml
service-data/
maven.ear.appxml.generate = true
service-ejb/
service-web/
Tip: container specific DD files can be stored at src/application/META-INF
xdocs/
sampleservice
project.xml
maven.xml
application/
service-data/
service-ejb/
service-web/
xdocs/
Customizing Maven
Use maven.xml
Override plugin goals
Intercept plugin goals with <preGoal/>
and <postGoal/>
Project Migration/Mavenizing
Can co-exist with ant
May require different directory structure
Too Slow?
Use console plugin
Summary
Pros
Work out-of-box for standard projects
Build assets highly reusable, thanks to
core/plugin architecture
Build rules are more dynamic
Best suited for project integration
IDE friendly
Cons
Incomplete documentation
Missing convenience details
Not yet mature. Still waiting for the R1.
Get More
http://maven.apache.org
http://www.onjava.com/pub/a/onjava/2003/10/22/maven.html
http://www-106.ibm.com/developerworks/java/library/j-maven
http://www.javausergroup.at/events/maven.pdf
http://www.theserverside.com/articles/article.jsp?l=MavenMagic
http://blogs.codehaus.org/people/vmassol/archives/000080.html
http://www.javaworld.com/javaworld/jw-10-2002/jw-1011maven.html
Questions