You are on page 1of 10

Maven2 Reference

Invoking Maven General Syntax:


mvn plugin:target [-Doption1 -Doption2 dots] mvn help mvn -X ...

Prints help debugging output, very useful to diagnose Creating a new Project (jar)
mvn archetype:create -DgroupId=Artifact Group -DartifactId=Artifact ID

Example:
mvn archetype:create -DgroupId=de.focusdv.bcs -DartifactId=new-app

Creates a new Project irectory new-app with pac!age structure de.focusdv.bcs" #ame of the pac!aged jar will be new$app$version"jar Creating a new Project (war)
mvn archetype:create -DgroupId=Artifact Group -DartifactId=Artifact ID -Darchetype rtifactId=maven-archetype-webapp

Example:
mvn archetype:create -DgroupId=de.focusdv.bcs -DartifactId=new-webapp -Darchetype rtifactId=maven-archetype-webapp

Creates a new irectory new-webapp with pac!age structure de.focusdv.bcs" #ame of the pac!aged war will be new$app$version"war Standard Project Structure directory %new$app%pom"xml %new$app%src% %new$app%src%main%java% %new$app%src%test%java% %new$app%src%main%resources% %new$app%src%test%resources% %new$app%target%classes% %new$app%target%test$classes% %new$app%target%dots description maven& project file Sources 'ava source tree 'ava unit tests 'ava classpath resources (esources for unit$tests compiles classes compiles test classes other plugins) output

%new$ webapp%src%main%webapp Compiling


mvn compile

root of webapp

Running nit !ests " Code Coverage


mvn test

compiles and runs unit tests


mvn clean cobertura:cobertura

generates a code$coverage report for the tests" *t only wor!s, if the pom"xml is configured as follows:
!"pro#ect$ % !build$ !plugins$ % !plugin$ !groupId$org.codehaus.mo#o!"groupId$ !artifactId$cobertura-maven-plugin!"artifactId$ !e&ecutions$ !e&ecution$ !goals$ !goal$clean!"goal$ !"goals$ !"e&ecution$ !"e&ecutions$ !"plugin$ % !"plugins$ !"build$ % !reporting$ !plugins$ !plugin$ !groupId$org.codehaus.mo#o!"groupId$ !artifactId$cobertura-maven-plugin!"artifactId$ !"plugin$ !"plugins$ !"reporting$ % !"pro#ect$

Packaging (jar# war)


mvn clean pac'age

compiles, runs unit tests and pac!ages the artifact +clean ma!es sure there are no unwanted files in the pac!age, Installing $rtifact in %ocal Repository
mvn clean install

compiles, runs unit tests, pac!ages and installs the artifact in the local repository" +User Home Directory%"m&%repository%, Installing &rdParty jar in local Repository
mvn install:install-file -Dfile=foo.#ar -DgroupId=org.foosoft -DartifactId=foo -Dversion=(.).* -Dpac'aging=#ar

Cleaning p
mvn clean

Creating 'clipse Project Structure


mvn eclipse:eclipse

*f using the eclipse plugin from update$site http:%%m&eclipse"codehaus"org remove the generated dependencies from project" Maven Project file (pom()ml) -inimal pom"xml is created with
mvn archetype:create

+see above," $dding *ependencies


!pro#ect$ % !dependencies$ !dependency$ !groupId$#unit!"groupId$ !artifactId$#unit!"artifactId$ !version$*.+.(!"version$ !scope$test!"scope$ !"dependency$ !dependency$ !groupId$org.springframewor'!"groupId$ !artifactId$spring!"artifactId$ !version$(.).,!"version$ !"dependency$ % !"dependencies$

.ecause of , junit will not be included in final pac!aging" $dding *evelopers


!pro#ect$ % !developers$ !developer$

!id$-aier!"id$ !name$.ans -aier!"name$ !email$hans.baier::at::focus-dv.de!"email$ !organi/ation$focus D0 1mb.!"organi/ation$ !roles$ !role$Developer!"role$ !"roles$ !"developer$ % !"developers$

Setting Compiler +ersion


!pro#ect$ % !build$ !plugins$ !plugin$ !artifactId$maven-compiler-plugin!"artifactId$ !configuration$ !source$(.2!"source$ !target$(.2!"target$ !"configuration$ !"plugin$ % !"plugins$ !"build$

$ssem,lies and Profiles

Creating $ssem,lies /o pac!age the artifact use the following lines in the "pom$file:
!plugin$ !artifactId$maven-assembly-plugin!"artifactId$ !configuration$ !descriptors$ !descriptor$src"main"assembly"foo-dep.&ml!"descriptor$ !descriptor$src"main"assembly"foo.&ml!"descriptor$ !"descriptors$ !"configuration$ !"plugin$

src%main%assembly is the maven standard directory for assemblies" /he first assembly descriptor pac!ages all dependencies into one jar:
!assembly$ !id$dep!"id$ !formats$ !format$#ar!"format$ !"formats$ !include-aseDirectory$false!"include-aseDirectory$ !dependency3ets$ !dependency3et$ !outputDirectory$!"outputDirectory$ !unpac'$true!"unpac'$ !scope$runtime!"scope$ !e&cludes$

!e&clude$#unit:#unit!"e&clude$ !"e&cludes$ !"dependency3et$ !"dependency3ets$ !"assembly$

/he second descriptor pac!ages the program:


!assembly$ !id$bin!"id$ !formats$ !format$/ip!"format$ !"formats$ !file3ets$ !file3et$ !directory$src"main"assembly"files!"directory$ !outputDirectory$!"outputDirectory$ !includes$ !include$44"4.bat!"include$ !include$44"native"44!"include$ !include$44"4.properties!"include$ !"includes$ !"file3et$ !file3et$ !directory$target!"directory$ !outputDirectory$!"outputDirectory$ !includes$ !include$4.#ar!"include$ !"includes$ !"file3et$ !"file3ets$ !"assembly$

Supplementary files in this example are in src%main%assembly%files" /his includes the program starter +"bat,, native libraries +%native, and Properties files" Pac!aging is invo!ed by:
mvn assembly:assembly

sing Profiles Profiles enable different versions of a project to be build, or adapting to different environments by an option on the command line" Profiles can modify almost all dependencies, plugins and settings in the pom.xml" *n cockpit-model they are used to generate a restricted demo$version and a release$ version li!e that:
!profiles$ !profile$ !id$release-profile!"id$ !dependencies$ !dependency$ !groupId$swt!"groupId$ !artifactId$swt-win*)!"artifactId$ !version$*.).(!"version$ !"dependency$ !"dependencies$ !build$ !filters$ !filter$src"main"filters"release0ersion.properties!"filter$

!"filters$ !"build$ !"profile$ !profile$ !id$demo!"id$ !dependencies$ !dependency$ !groupId$swt!"groupId$ !artifactId$swt-win*)!"artifactId$ !version$*.).(!"version$ !"dependency$ !"dependencies$ !build$ !filters$ !filter$src"main"filters"demo0ersion.properties!"filter$ !"filters$ !"build$ !"profile$ % !"profiles$

0ere the release-profile uses the windows library of S1/ +since our customers) platform is windows +li!e it or not""",, and substitutes the resources files) placeholders with the variables in releaseVersion.properties" /he demo$profile is almost the same except it uses demoVersion.properties for filtering" sagemvn -5release-profile clean assembly:assembly

or
mvn -5demo clean assembly:assembly

sing Profiles ,y .S *n this example we want to use the 2inux S1/ 2ibraries on 2inux and the 1indows libs on 1indows:
!profiles$ !profile$ !id$windows!"id$ !activation$ !os$ !family$windows!"family$ !"os$ !"activation$ !dependencies$ !dependency$ !groupId$swt!"groupId$ !artifactId$swt-win*)!"artifactId$ !version$*.(.(!"version$ !"dependency$ !"dependencies$ !"profile$ !profile$ !id$uni&!"id$ !activation$ !os$ !family$uni&!"family$ !"os$ !"activation$ !dependencies$

!dependency$ !groupId$swt!"groupId$ !artifactId$swt-linu&-gt'!"artifactId$ !version$*.(.(!"version$ !"dependency$ !"dependencies$ !"profile$ !"profiles$

+ersioning# Repositories and Releases

Setting Source Code Control System


!pro#ect$ % !scm$ !developer6onnection$ scm:svn:https:""svnhost.net"svnroot"trun'"new-app !"developer6onnection$ !"scm$ !build$ !plugins$ !plugin$ !artifactId$maven-release-plugin!"artifactId$ !configuration$ !tag-ase$ https:""svnhost.net"svnroot"tags !"tag-ase$ !"configuration$ !"plugin$ % !"plugins$ !"build$

+ersioning 3eep the 4erision of your P5- artifact in the form version$S#6PS05/ until you release" -avens release plugin then removes the $S#6PS05/ suffix" sing internal Repositories /his assumes that a machine myhost exists with a configured and running 1eb$Server and SS0$ Server
!repositories$ !repository$ !id$focus-repository!"id$ !name$7ocus -63 8epository!"name$ !url$http:""myhost"mvn"repository!"url$ !"repository$ !"repositories$ !distribution9anagement$ !repository$ !id$focus-repository!"id$ !name$7ocus -63 8epository!"name$ !url$scp:""myhost"var"www"mvn"repository"!"url$ !"repository$ !"distribution9anagement$

Installing $rtifact in Remote Repository


mvn clean deploy

compiles, runs unit tests, pac!ages and installs the artifact in the remote repository" Install &rdParty jar to Remote Repository
mvn deploy:deploy-file -DgroupId=commons-collections -DartifactId=collections-generic -Dversion=:.; -Dpac'aging=#ar -Dfile=collections-generic-:.;.#ar -DrepositoryId=focus-repository -Durl=scp:""host"home"mvn"public<html"repository

Preparing Releases -a!e sure, the SC- settings in the P5- are correct and all changes are committed to the SC-" /hen execute
mvn -Dusername=USER -Dpassword=PASS release:prepare

.efore issuing the above command use it with -DdryRun=true first tags in configured build profiles in the pom"xml Performing Releases
mvn -5 profile -Drelease:perform

Chec!s out the released version from tag in repository, builds, tests, pac!ages and installs pac!age, javadoc and sources in repository" 6s preparing the release removes activation tags from build profiles, it is necessary to supply the profile or the release will fail"

/e,0*evelopment

Integration0!est wit1 tomcat


!pro#ect$ % !build$ !plugins$ % !plugin$ !groupId$org.codehaus.cargo!"groupId$ !artifactId$cargo-maven)-plugin!"artifactId$ !e&ecutions$ !e&ecution$ !id$tomcat-e&ecution!"id$ !phase$pac'age!"phase$ !goals$ !goal$start!"goal$ !"goals$ !configuration$ !wait$true!"wait$

!container$ !containerId$tomcat2&!"containerId$ !/ip=rlInstaller$ !url$!http:""www.apache.org"..."#a'arta-tomcat./ip$!"url$ !installDir$>?installDir@!"installDir$ !"/ip=rlInstaller$ !"container$ !configuration$ !dir$>?pro#ect.build.directory@"tomcat2&"!"dir$ !"configuration$ !"configuration$ !"e&ecution$ !"e&ecutions$ !"plugin$ !"plugins$ !"build$

/hen execute in project directory:


mvn -X integration-test

/he war$file will built, tested and pac!aged" /hen tomcat will be downloaded, installed and started with the war$file of the project deployed to the server" *f you want to use jetty7 +already embedded, fast startup, use:
mvn cargo:start

+Press Ctrl$C to stop, .nline we,0development wit1 2etty plugin 6dd -aven$Plugin to pom"xml:
!plugins$ % !plugin$ !groupId$org.mortbay.#etty!"groupId$ !artifactId$maven-#etty,-plugin!"artifactId$ !configuration$ !scanInterval3econds$(;!"scanInterval3econds$ !"configuration$ !"plugin$ % !"plugins$

/hen run 'etty with


mvn #etty,:run

.nline we,0development and automatic deployment wit1 tomcat plugin 6dd -aven$Plugin to pom"xml:
!plugins$ % !plugin$ !groupId$org.codehaus.mo#o!"groupId$ !artifactId$tomcat-maven-plugin!"artifactId$ !configuration$ !url$http:""(A).(,+.()A.*,:+;+;"manager"html!"url$ !"configuration$ !"plugin$

!plugin$ !groupId$org.codehaus.cargo!"groupId$ !artifactId$cargo-maven)-plugin!"artifactId$ !"plugin$ % !"plugins$ % !repositories$ !repository$ !id$codehaus!"id$ !name$6odehaus maven repository!"name$ !url$http:""dist.codehaus.org"!"url$ !layout$legacy!"layout$ !"repository$ % !"repositories$

/hen run /omcat with


mvn tomcat:run

eploy the war automatically with


mvn tomcat:deploy

*f already deployed, the webapp needs to be undeployed first:


mvn tomcat:undeploy

#ote that automatic deployment%undeployment only wor!s without further configuration in !"V#$%&H'!#(conf(settin)s.xml if the managers username is admin with empty password

You might also like