Title: Building and Managing Java Projects with Maven
1Building and Managing Java Projects with Maven
2Agenda
- What is Maven?
- A J2EE example
- Customizing and extending Maven
- Tips and discussions
3 What is Maven?
- A Java project management and integration build
tool. - Based on the concept of XML Project Object Model
(POM). - Originally developed for building Turbine.
- A small core with numerous plugins (in Jelly).
4 Build Tools Retrospective
- One level above ant.
- Make ? ant ? Maven
- (Assembly ? C ? C)
Make makefile target
Ant build.xml target
Maven project.xml maven.xml goals
5Under the Hood
jalopy
site
junit
etc
test
cactus
artifact
xdoc
ear
gump
xdoclet
javadoc
java
jar
war
ejb
plugins
maven core, jelly, werkz
ant
forehead classloader util launcher
Maven
6Architecture Overview
Build System
project.xml (pom)
http
Remote Repo
maven.xml (goals)
Local Repo
maven
Site
7Artifact Repository
- The Most Important Feature
- Remote Repository
mave.repo.remote/ltgroupIdgt/lttypegts/ltartifactIdgt
-ltversiongt.lttypegt
- repository
-
- xalan
- jars
- xalan-2.5.0.jar
- xalan-2.5.1.jar
-
-
ltdependenciesgt ltdependencygt
ltgroupIdgtxalanlt/groupIdgt
ltartifactIdgtxalanlt/artifactIdgt
ltversiongt2.5.1lt/versiongt lttypegtjarlt/typegt
lt/dependencygt lt/dependenciesgt
8Artifact Repository
- Local Repository
- A local mirror/cache of downloaded artifacts from
remote repositories. - Located at user.home/.maven/repository
mave.repo.local/ltgroupIdgt/lttypegts/ltartifactIdgt-
ltversiongt.lttypegt
9Implementing an Example
- Get Started
- Download from
- http//maven.apache.org/start/download.html
- Current version 1.0rc1
- Environment setup
- export MAVEN_HOMEc/maven-1.0rc1
- export PATHMAVEN_HOME/binPATH
- (or set MAVEN_HOME c\maven-1.0rc1
- set PATH MAVEN_HOME\binPATH )
- run install_repo.sh to populate the local
repository
10 Create a New Project
- Type
- maven genapp
- It will prompt for
- project id
- project name
- project package name
- A Sample Service J2EE Project
- EJB (stateless session beans exposed as web
services) - Data components
- Web application
11Directory Layout
- Project Directory Layout
- sampleservice
- project.xml - Master POM of the project
- maven.xml - Reactor definition
- project.properties - Properties related to the
project - application/ - Application component
- service-data/ - Common data component
- service-ejb/ - EJB/WS component
- service-web/ - Web Application component
- target/ - Generated artifact directory
- xdocs/ - Various documents in xml format
12Directory Layout
- A Component Directory Layout
-
- service-data - Data component subproject
- project.xml - POM of the data project
- maven.xml - Goals definition
- project.properties - Properties related to the
project - src/ - Source directory
- conf/ - Configuration and resource files
- java/ - Java source files
- test/ - Test source files
- target/ - Generated artifact directory
- xdocs/ - Various documents in xml format
13Project Object Model (POM)
- Projects are described as Project Object Model.
- Project Management
- Detailed description of the project.
- Company information.
- Developer roles and information.
- Mailing list and source control modules
configuration. - Project Build
- Source code and test code location.
- Resources location
14Project Object Model (POM)
- Project Dependency
- Libraries needed for build and runtime.
- Project Reports
- Junit reports
- Javadoc reports
- Checkstyle reports, .etc
15Project Management Section
- ltprojectgt
- ltpomVersiongt3lt/pomVersiongt
- ltgroupIdgtsampleservicelt/groupIdgt
- ltnamegtSample Servicelt/namegt lt!-- Used in
Javadoc --gt - ltidgtsampleservicelt/idgt
- ltcurrentVersiongt1.0lt/currentVersiongt
- lt!-- Used for document creation --gt
- ltorganizationgt
- ltnamegtMy, Inc.lt/namegt
- lturlgthttp//www.myinc.comlt/urlgt
- ltlogogt/images/logo.giflt/logogt
- lt/organizationgt
-
(elements in bold are required)
16Project Management Section
- ltprojectgt
-
- ltinceptionYeargt2003lt/inceptionYeargt lt!-- Used in
JavaDoc - ltpackagegtcom.myinc.sampleservicelt/packagegt lt!--
Used in JavaDoc --gt - ltshortDescriptiongtDemo to use mavenlt/shortDescript
iongt lt!-- one liner --gt - lt!-- Used in front page--gt
- ltdescriptiongt
- A detailed description about this demo
- lt/descriptiongt
- lturlgthttp//www.myinc.com/sampleservice/lt/urlgt
- ltissueTrackingUrl/gt
- ltsiteAddressgtdev.myinc.comlt/siteAddressgt lt!--
Used in deployment --gt - ltsiteDirectorygt/www/sampleservice/lt/siteDirectorygt
lt!-- Used in deployment --gt - lt!-- Used in deployment. If defined, it overrides
maven.repo.central --gt - ltdistributionSitegt/www/dist/sampleservicelt/distrib
utionSitegt - lt!-- Used in deployment, final distribution
directory --gt - ltdistributionDirectorygt/www/www.myinc.com/somedirlt
/distributionDirectorygt
17Project Management Section
- ltprojectgt
-
- ltrepositorygt
- ltconnectiongtscmcvspserveranoncvs_at_cvs.myinc.com
/cvsrootsampleservicelt/connectiongt - ltdeveloperConnectiongtscmcvspservermaven.usern
ame_at_cvs.myinc.com/cvsrootsampleservicelt/develop
erConnectiongt - lturlgthttp//cvs.myinc.org/viewcvs/sampleservice/lt/
urlgt - lt/repositorygt
- lt!-- Used in mavendist --gt
- ltversionsgt
- ltversiongt
- ltidgt1.0-beta-1lt/idgt
- ltnamegt1.0-beta-1lt/namegt
- lttaggt1.0-beta-1lt/taggt
- lt/versiongt
- lt/versionsgt
- ltbranches/gt
ltmailingLists/gt ltcontributors/gt ltdevelopers/gt
18 Project Dependency Section
- ltprojectgt
-
- ltdependenciesgt
- ltdependencygt
- ltgroupIdgtlog4jlt/groupIdgt
- ltartifactIdgtlog4jlt/artifactIdgt
- ltversiongt1.2.8lt/versiongt
- ltpropertiesgt
- ltear.bundlegttruelt/ear.bundlegt
- ltejb.manifest.classpathgttruelt/ejb.manifest.classpa
thgt - lt/propertiesgt
- lt/dependencygt
- lt/dependenciesgt
-
Special Dependency SNAPSHOT
19 Project Dependency Section
- Dependency Classloader
-
- ltdependencygt
- ltgroupIdgtbcellt/groupIdgt
- ltartifactIdgtbcellt/artifactIdgt
- ltversiongt5.1lt/versiongt
- ltpropertiesgt
- ltclassloadergtrootlt/classloadergt
- lt/propertiesgt
- lt/dependencygt
Maven has three classloaders root -- ant
classloader root.maven maven core
classloader default plugin classloader
20 Project Dependency Section
- Dependency Override
- project.xml
-
- ltdependencygt
- ltgroupIdgtweblogiclt/groupIdgt
- ltartifactIdgtweblogiclt/artifactIdgt
- ltversiongt8.1.1lt/versiongt
- ltpropertiesgt
- ltclassloadergtrootlt/classloadergt
- lt/propertiesgt
- lt/dependencygt
- project.properties
-
- Dependency override
- maven.jar.override on
- maven.jar.weblogic weblogic.home/lib/weblogic
.jar - maven.jar.webservices weblogic.home/lib/webse
rvices.jar
21 Project Build Section
- Defines the location of source, test and resource
files. -
- ltbuildgt
- ltnagEmailAddressgtbuildmaster_at_myinc.comlt/nagEmailAd
dressgt - ltsourceDirectorygtsrc.java.dirlt/sourceDirectorygt
- ltunitTestSourceDirectorygtsrc.test.dirlt/unitTest
SourceDirectorygt - ltaspectSourceDirectory/gt
-
-
src/java
src/test
22 Project Build Section
- ltunitTestgt
- ltincludesgt
- ltincludegt/Test.javalt/includegt
- lt/includesgt
- ltresources/gt
- lt/unitTestgt
- ltresourcesgt
- ltresourcegt
- ltdirectorygtsrc.conf.dirlt/directorygt
- lttargetPath/gt
- ltincludesgt
- ltincludegt/.propertieslt/includegt
- lt/includesgt
- lt/resourcegt
- lt/resourcesgt
src/conf
prefix package name, e.g. com.myinc. sampleservice
23 Project Report Section
- Defines various reports to be generated
- ltreportsgt
- ltreportgtmaven-jdepend-pluginlt/reportgt
- ltreportgtmaven-checkstyle-pluginlt/reportgt
- ltreportgtmaven-changelog-pluginlt/reportgt
- ltreportgtmaven-developer-activity-pluginlt/reportgt
- ltreportgtmaven-file-activity-pluginlt/reportgt
- ltreportgtmaven-javadoc-pluginlt/reportgt
- ltreportgtmaven-jxr-pluginlt/reportgt
- ltreportgtmaven-junit-report-pluginlt/reportgt
- ltreportgtmaven-linkcheck-pluginlt/reportgt
- ltreportgtmaven-tasklist-pluginlt/reportgt
- lt/reportsgt
24 Project Report - Example
Jakarta Turbine
25 Project Report - XDoc
xdocs/navigation.xml ltmenu name"General
Information"gt ltitem name"Overview"
href"/index.html"/gt ltitem
name"Features" href"/features.htm
l"/gt ltitem name"Specification"
href"/fsd.html"/gt ltitem name"Getting
Started" href"/getting-started.html"/gt
lt/menugt
xdocs/features.xml ltdocumentgt
ltpropertiesgt lttitlegtTurbine
Featureslt/titlegt ltauthor
email""gtJon S. Stevenslt/authorgt
lt/propertiesgt ltbodygt
ltsection name"Features"gt ltpgtThis
document is for bragging about all of Turbine's
. lt/documentgt
26Property 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 Dfoobar
The last definition wins
Sample build.properties bea.homec/bea81sp1 or
acle.homec/oracle/ora9i
27 Build Process Design
- Sample Service project components
Common data module EJBs Web Application
Data (jar)
EJB (jar)
Webapp (war)
EAR
28Subproject service-data
project.xml (POM) ltprojectgt ltextendgt../project.x
mllt/extendgt ltnamegtSample Service Data
Modulelt/namegt ltidgtsampleservice-datalt/idgt
lt/projectgt Note POM can be inherited.
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- project.xml
- maven.xml
- src/
- service-ejb/
- service-web/
- xdocs/
29Subproject service-data
maven.xml (Goals) ltproject default"build"
xmlnsj"jellycore" xmlnsmaven"jellymaven"
xmlnsant"jellyant"gt ltgoal
name"build" prereqs"jarinstall"/gt lt/projectgt
Note Goals can also be inherited
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- project.xml
- maven.xml
- src/
- service-ejb/
- service-web/
- xdocs/
Make jar
Local Repo
30Subproject service-ejb
project.xml (POM) ltprojectgt ltextendgt../project.x
mllt/extendgt ltnamegtSample Service EJBlt/namegt
ltidgtsampleservice-ejblt/idgt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-datalt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
ltpropertiesgt ltejb.manifest.classpathgttrue
lt/ejb.manifest.classpathgt lt/propertiesgt
lt/dependencygt lt/projectgt
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- service-ejb/
- project.xml
- maven.xml
- src/
- service-web/
- xdocs/
Local Repo
xdoclet
compile
ws-gen
ejb-jar
31Subproject service-ejb
- Maven XDoclet Plugin
- Support all standard tags
- Automatically includes generated src dir in
compilation src set - Support util class generation
- Dependencies
- xdoclet-ejb-module-1.2
- xdoclet-web-module-1.2
- xdoclet-bea-module-1.2
- (for WebLogic Server Deployment)
Note Current version 1.2 does not work
out-of-box (needed to fix the project.xml)
32Subproject service-ejb
Maven XDoclet Plugin - Properties EJBDoclet
maven.xdoclet.ejbdoclet.fileset.0.include/ejb/
/Bean.java maven.xdoclet.ejbdoclet.ejbSpec2.0
maven.xdoclet.ejbdoclet.verbosetrue maven.xdoclet
.ejbdoclet.session.0false maven.xdoclet.ejbdoclet
.localhomeinterface.0true maven.xdoclet.ejbdoclet
.localinterface.0true maven.xdoclet.ejbdoclet.uti
lobject.0true EJBDoclet WebLogic Nested
Element maven.xdoclet.ejbdoclet.weblogic.0true ma
ven.xdoclet.ejbdoclet.weblogic.0.mergeDirsrc.di
r/ejbdoclet maven.xdoclet.ejbdoclet.weblogic.0.de
stDirmaven.xdoclet.ejbdoclet.deploymentdescript
or.0.destDir
33Subproject service-ejb
- Web Services
- Currently Maven has no container specific plugins
ltgoal name"wsgen" prereqs"wsgen.autotype,
wsgen.source2wsdd"/gt ltgoal name"wsgen.autotype"
gt lttaskdef name"autotype"
classname"weblogic.ant.taskdefs.webservices.java
schema.JavaSchema"/gt . lt/goalgt ltgoal
namewsgen.source2wsddgt lt/goalgt
project.properties Web Services
maven.webservice.javaComponents\
com.myinc.sampleservice.ejb.BasicAccountInquiry,\
com.myinc.sampleservice.ejb.ExpandedAccountInq
uiry,\ com.myinc.sampleservice.ejb.DetailedAcc
ountInquiry maven.webservice.autotype.packagecom.
myinc.sampleservice.autotype maven.webservice.auto
type.keepgeneratedfalse
Use tag _at_wlws
34Subproject service-ejb
- Maven EJB Plugin
- Currently no container specific ejb-jar
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- service-ejb/
- project.xml
- maven.xml
- src/
- service-web/
- xdocs/
ltgoal name"ejbjar" prereqs"javacompile"gt
ltpath id"ejb.classpath"gt ltpathelement
location"maven.build.dest"/gt ltpath
refid"maven.dependency.classpath"/gt lt/pathgt
ltjset var"maven.ejb.descriptordir"
value"pom.getPluginContext('maven
-xdoclet-plugin')./gt ltejbjar
srcdir"maven.build.dest"
descriptordir"maven.ejb.descriptordir"
flatdestdir"true"
basejarname"pom.artifactId-pom.currentVers
ion"gt ltclasspath refid"ejb.classpath"/gt
ltweblogic destdir"maven.build.di
rnewCMP"true"
outputdir"maven.build.dir/ejb"
rebuild"false"
ejbcclass"weblogic.ejbc" lt/weblogicgt
35Subproject service-ejb
Making the Final EJB Jar
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- service-ejb/
- project.xml
- maven.xml
- src/
- service-web/
- xdocs/
ltproject default"buildgt ltgoal name"build"
prereqs"ejbinstall"/gt ltpreGoal
name"javacompile"gt ltattainGoal
name"xdocletejbdoclet"/gt lt/preGoalgt
ltpostGoal name"javacompile"gt ltattainGoal
name"wsgen"/gt ltattainGoal
name"javajar-resources"/gt lt/postGoalgt
ltpreGoal name"ejbejb"gt ltattainGoal
name"ejbjar"/gt lt/preGoalgt ltpostGoal
name"ejbinstall"gt ltartifactinstall
artifact"maven.build.dir/pom.artifactId
-pom.currentVersion.xml" type"xml
project"pom"/gt lt/postGoalgt
36Subproject service-web
project.xml (POM)
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- service-ejb/
- service-web/
- project.xml
- maven.xml
- src/
- xdocs/
ltprojectgt ltextendgt../project.xmllt/extendgt
ltnamegtSample Service Web Applicationlt/namegt
ltidgtsampleservice-weblt/idgt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-datalt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
lt/dependencygt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-ejblt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
lttypegtejblt/typegt ltpropertiesgt
ltweb-servicegttruelt/web-servicegt
lt/propertiesgt lt/dependencygt
custom property
37Subproject service-web
maven.xml (goals)
ltproject defaultbuildgt ltgoal name"build"
prereqs"warinstall"/gt ltpreGoal name"warwar"gt
ltjforEach var"dep"
items"pom.dependencies"gt
ltjif test"dep.getProperty('web-service')'tr
ue'"gt ltutilfile var"xmlFile"
name"maven.repo.loca
l/dep.groupId/xmls/dep.artifactId-
dep.version.xml"/gt
ltjif test"xmlFile.exists()"gt
ltxparse var"xml"
xml"xmlFile"/gt
ltxforEach var"node" select"xml//"gt
ltjset var"temp" value"root.add(node.de
tach())"/gt lt/xforEachgt
lt/jifgt lt/jifgt
lt/jforEachgt ltjif
test"!root.elements().isEmpty()"gt
lt! output the web-services.xml --gt lt/jifgt
lt/preGoalgt
preGoal is used to generate web services DD
file if defined
38Subproject application
lt!-- EJB Component --gt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-ejblt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
lttypegtejblt/typegt ltpropertiesgt
ltear.bundlegttruelt/ear.bundlegt
lt/propertiesgt lt/dependencygt lt!-- WAR
Component --gt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-weblt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
lttypegtwarlt/typegt ltpropertiesgt
ltear.bundlegttruelt/ear.bundlegt
ltear.appxml.war.context-rootgt
/pom.artifactId lt/ear.appxml.war.conte
xt-rootgt lt/propertiesgt lt/dependencygt
EAR Packaging ltprojectgt ltextendgt../project.
xmllt/extendgt ltnamegtSample Service
Applicationlt/namegt ltidgtsampleservicelt/idgt
lt!-- Data Component --gt ltdependencygt
ltgroupIdgtpom.groupIdlt/groupIdgt
ltartifactIdgtsampleservice-datalt/artifactIdgt
ltversiongtpom.currentVersionlt/versiongt
lttypegtjarlt/typegt ltpropertiesgt
ltear.bundlegttruelt/ear.bundlegt
lt/propertiesgt lt/dependencygt
- sampleservice
- project.xml
- maven.xml
- application/
- project.xml
- maven.xml
- src/
- service-data/
- service-ejb/
- service-web/
- xdocs/
39Subproject application
EAR Packaging ltproject defaultbuildgt
ltgoal name"build" prereqs"ear"/gt ltpreGoal
name"eargenerate-ear-descriptor"gt ltmkdir
dir"maven.ear.appxml.dir"/gt
lt/preGoalgt lt/projectgt
- sampleservice
- project.xml
- maven.xml
- application/
- project.xml
- maven.xml
- src/
- service-data/
- service-ejb/
- service-web/
- xdocs/
project.properties Ear related maven.ear.appx
ml.dir maven.build.dir/application/META-INF m
aven.ear.appxml maven.ear.appxml.dir/applicat
ion.xml maven.ear.appxml.generate true
Tip container specific DD files can be stored at
src/application/META-INF
40Putting It Together Reactor
ltproject defaultbuild xmlnsj"jellycore"
xmlnsmaven"jellymaven"
xmlnsant"jellyant"gt .
- sampleservice
- project.xml
- maven.xml
- application/
- service-data/
- service-ejb/
- service-web/
- xdocs/
ltgoal name"build"gt ltmavenreactor
basedir"basedir" postProcessing"true"
includes"/project.xml" excludes""
goals"build" banner"Building"
ignoreFailures"false"/gt lt/goalgt
41Putting It Together Reactor
/c/dev/sampleservice/gtmaven __ __ \/ __
_Apache__ ___ \/ / _ \ V / -_) ' \
intelligent projects _ _\__,_\_/\_____
v. 1.0-rc1-SNAPSHOT Starting the reactor... Our
processing order Sample Service Data
Module Sample Service EJB Sample Service Web
Application Sample Service Application ----------
------------------------------ Building Sample
Service Data Module Memory 3M/4M -------------
--------------------------- build javaprepare-f
ilesystem mkdir Created dir
C\dev\sampleservice\service-data\target\classes
javacompile .
42Customizing Maven
- Override plugin properties in
- project.properties
- build.properties
- Use maven.xml
- Override plugin goals
- Intercept plugin goals with ltpreGoal/gt and
ltpostGoal/gt - Write you own plugin
- In Java, Jelly, or other scripting language.
43Real Life Maven
- Single artifact per project
- Can be tweaked to deliver multiple artifacts as
long as no type conflicts - Fine-grained design
- Project Migration/Mavenizing
- Can co-exist with ant
- May require different directory structure
- Too Slow?
- Use console plugin
- Are you ready for maven?
- Culture change
44Summary
- 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.
45Get More
- http//maven.apache.org
- http//www.onjava.com/pub/a/onjava/2003/10/22/mave
n.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?
lMavenMagic - http//blogs.codehaus.org/people/vmassol/archives/
000080.html - http//www.javaworld.com/javaworld/jw-10-2002/jw-1
011-maven.html
46Questions