Upload
samuel-langlois
View
705
Download
0
Tags:
Embed Size (px)
Citation preview
Maven overviewSamuel Langlois – November 2012
Maven history
• Started by Jason Van Zyl in 2001!o Standardising the build of Apache Turbine
• Version historyo 1.0 in July 2004, after loads of betaso 2.0 in October 2005, completely redesignedo 3.0 in October 2010, fully compatibleo 3.0.4 is the latest
• Today controlled by Sonatypeo Future: "Tesla"?
Maven is *not* a better Ant
<project>
<target name="compile">
<javac ...>
</target>
<target name="test">
<junit ..>
</target>
<target name="package">
<jar ..>
</target>
</project>
<project>
<groupId>org.alfresco</><artifactId>alfresco-core</><version>4.1.0</><name>Alfresco Core</>
<dependencies> <dependency>
<groupId>commons-logging</><artifactId>commons-
logging</><version>1.1</>
</dependency>
</dependencies>
</project>
build.xml pom.xml
Maven is *not* a better Ant
<project>
<target name="compile">
<javac ...>
</target>
<target name="test">
<junit ..>
</target>
<target name="package">
<jar ..>
</target>
</project>
build.xml pom.xml
CODE
<project>
<groupId>org.alfresco</><artifactId>alfresco-core</><version>4.1.0</><name>Alfresco Core</>
<dependencies> <dependency>
<groupId>commons-logging</><artifactId>commons-
logging</><version>1.1</>
</dependency>
</dependencies>
</project>
DATA
Maven plugins
• Written in Java (MOJO), or ...
• Entry-points are called goals
• Examples:o Core
maven-compiler-plugin maven-resources-plugin - including filtering maven-surefire-plugin - executes tests
o Reporting maven-javadoc-plugin maven-findbugs-plugin
o Code Generation antlr3-maven-plugin
o maven-alfresco-plugin !!o .......
Configuring a plug-in
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<source>1.6</source>
<debug>false</debug>
</configuration>
</plugin>
Maven phasesPhase Default binding for jar packaging
initialize
generate-sources
process-resources resources:resources
compile compiler:compile
generate-test-sources
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
pre-integration-test
integration-test
post-integration-test
install install:install
deploy deploy:deploy
Plugging a plug-in<plugin>
<artifactId>maven-jetty-plugin</artifactId>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals> <goal>run</goal> </goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals> <goal>stop</goal> </goals>
</execution>
</executions>
<configuration>
<port>9876</port>
</configuration>
</plugin>
Maven conventions
• tree layouto pom.xmlo src
main• java• resources
test• java• resources
o target
• tests are executed as part of the build
Follow the Maven way!
Maven dependency
A dependency (internal or external) is made of:
• mandatory : GAVo groupId (org.alfresco)o artifactId (alfresco-datamodel)o version
release: 4.1.0 snapshot: 4.1-SNAPSHOT
• optionally:o scope (compile, test, provided, ...)o classifier (jdk6, sources, javadocs, ...)
Dependencies are transitive!• mvn dependency:tree dependency:list
Maven dependency management
mvnlocal Maven repo~/.m2/repository
Maven Central http://repo.maven.apache.org/maven2/
mvn install
Maven dependency management
mvnmvn install
Team Repo
Maven Centralhttp://repo.maven.apache.org/maven2/
mvn deploy
local Maven repo~/.m2/repository
Invoking Maven
• call a phaseo mvn clean installo mvn test
• call a goalo mvn surefire:testo mvn javadoc:javadoc
• Useful switcheso -o : offlineo -U : force check for snapshot updateso -X : verbose, to check plugin configuration
• Adding command-line params for pluginso -DskipTestso -Dtest=MyClassTest
Multimodule builds
• call sub-projects in parent folder<packaging>pom</packaging><modules> <module>core</module> <module>web-client</module></modules>
• pom inheritance in sub-projects<parent> <groupId>... <artifactId>... <version>...</parent>
• computing the actual pom• mvn help:effective-pom
Maven profiles
• conditional parts in the pom.xmlo plug/unplug a submoduleo run a different set of testso test various platformso ...
• Triggering a profileo command line: -P profile1,!profile3o presence/absence of a propertyo automatically: JDK, OS, platform, existence of a
file, ...
IDE integration
• "Old school": Maven generating IDE projecto mvn eclipse:eclipse [-DdownloadSources=true]o mvn idea:ideao mvn netbeans:netbeans
• Recommended: IDE directly reading pom.xmlo m2e plugin, included in "Eclipse IDE for Java Devs"
Reporting - Maven site
Reporting - Sonar
Want some more?
• Maven "official" doc from Sonatypeo Maven by Exampleo Reference
• pom.xml reference
• Maven Plugins referenceo Apacheo Codehaus