maven overview

19
Maven overview Samuel Langlois – November 2012

Upload: samuel-langlois

Post on 12-May-2015

705 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Maven overview

Maven overviewSamuel Langlois – November 2012

Page 2: Maven overview

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"?

Page 3: Maven overview

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

Page 4: Maven overview

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

Page 5: Maven overview

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 .......

Page 6: Maven overview

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>

Page 7: Maven overview

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

Page 8: Maven overview

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>

Page 9: Maven overview

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!

Page 10: Maven overview

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

Page 11: Maven overview

Maven dependency management

mvnlocal Maven repo~/.m2/repository

Maven Central http://repo.maven.apache.org/maven2/

mvn install

Page 12: Maven overview

Maven dependency management

mvnmvn install

Team Repo

Maven Centralhttp://repo.maven.apache.org/maven2/

mvn deploy

local Maven repo~/.m2/repository

Page 13: Maven overview

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

Page 14: Maven overview

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

Page 15: Maven overview

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, ...

Page 16: Maven overview

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"

Page 17: Maven overview

Reporting - Maven site

Page 18: Maven overview

Reporting - Sonar

Page 19: Maven overview

Want some more?

• Maven "official" doc from Sonatypeo Maven by Exampleo Reference

• pom.xml reference

• Maven Plugins referenceo Apacheo Codehaus