Maven overview
Samuel Langlois – May 2017
Apache Maven history
• Started by Jason Van Zyl in 2001
o Standardising the build of Apache Turbine
• Version history
o 1.0 in Jul-2004, after loads of betas
o 2.0 in Oct-2005, completely redesigned
o 3.0 in Oct-2010, fully compatible
o 3.5.0 is the latest, released Apr-2017
• Previsouly somewhat controlled by Sonatype
• Strong and stable!
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>5.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>5.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.6</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<debug>false</debug>
</configuration>
</plugin>
Maven phases
Phase 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 layout
o pom.xml
o 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 : GAV
o groupId (org.alfresco)
o artifactId (alfresco-datamodel)
o version
 release: 4.1.0
 snapshot: 4.1.0-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 Central
http://repo.maven.apache.org/maven2/
local Maven repo
~/.m2/repository
Invoking Maven
• call a phase
o mvn clean install
o mvn test
• call a goal
o mvn surefire:test
o mvn javadoc:javadoc
• Useful switches
o -o : offline
o -U : force check for snapshot updates
o -X : verbose, to check plugin configuration
• Adding command-line params for plugins
o -DskipTests
o -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.xml
o plug/unplug a submodule
o run a different set of tests
o test various platforms
o ...
• Triggering a profile
o command line: -Pprofile1,!profile3
o presence/absence of a property
o automatic: JDK, OS, platform, existence of a file, …
IDE integration
• "Old school": Maven generating IDE project
o mvn eclipse:eclipse [-DdownloadSources=true]
o mvn idea:idea
o mvn netbeans:netbeans
• Recommended: IDE directly reading pom.xml
o m2e plugin, included in "Eclipse IDE for Java Devs"
Reporting - Maven site
Reporting - Sonar
Want some more?
• Maven books from Sonatype
https://www.sonatype.com/ebooks
• pom.xml reference
• Maven Plugins reference
o Apache
o Codehaus

Maven overview

  • 1.
  • 2.
    Apache Maven history •Started by Jason Van Zyl in 2001 o Standardising the build of Apache Turbine • Version history o 1.0 in Jul-2004, after loads of betas o 2.0 in Oct-2005, completely redesigned o 3.0 in Oct-2010, fully compatible o 3.5.0 is the latest, released Apr-2017 • Previsouly somewhat controlled by Sonatype • Strong and stable!
  • 3.
    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>5.1.0</> <name>Alfresco Core</> <dependencies> <dependency> <groupId>commons-logging</> <artifactId>commons-logging</> <version>1.1</> </dependency> </dependencies> </project> build.xml pom.xml
  • 4.
    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>5.1.0</> <name>Alfresco Core</> <dependencies> <dependency> <groupId>commons-logging</> <artifactId>commons-logging</> <version>1.1</> </dependency> </dependencies> </project> DATA
  • 5.
    Maven plugins • Writtenin 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 .......
  • 6.
  • 7.
    Maven phases Phase Defaultbinding 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
  • 8.
    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>
  • 9.
    Maven conventions • treelayout o pom.xml o src  main • java • resources  test • java • resources o target • tests are executed as part of the build Follow the Maven way!
  • 10.
    Maven dependency A dependency(internal or external) is made of: • mandatory : GAV o groupId (org.alfresco) o artifactId (alfresco-datamodel) o version  release: 4.1.0  snapshot: 4.1.0-SNAPSHOT • optionally: o scope (compile, test, provided, ...) o classifier (jdk6, sources, javadocs, ...) Dependencies are transitive! • mvn dependency:tree dependency:list
  • 11.
    Maven dependency management mvnlocalMaven repo ~/.m2/repository Maven Central http://repo.maven.apache.org/maven2/ mvn install
  • 12.
    Maven dependency management mvnmvninstall Team Repo Maven Central http://repo.maven.apache.org/maven2/ local Maven repo ~/.m2/repository
  • 13.
    Invoking Maven • calla phase o mvn clean install o mvn test • call a goal o mvn surefire:test o mvn javadoc:javadoc • Useful switches o -o : offline o -U : force check for snapshot updates o -X : verbose, to check plugin configuration • Adding command-line params for plugins o -DskipTests o -Dtest=MyClassTest
  • 14.
    Multimodule builds • callsub-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
  • 15.
    Maven profiles • conditionalparts in the pom.xml o plug/unplug a submodule o run a different set of tests o test various platforms o ... • Triggering a profile o command line: -Pprofile1,!profile3 o presence/absence of a property o automatic: JDK, OS, platform, existence of a file, …
  • 16.
    IDE integration • "Oldschool": Maven generating IDE project o mvn eclipse:eclipse [-DdownloadSources=true] o mvn idea:idea o mvn netbeans:netbeans • Recommended: IDE directly reading pom.xml o m2e plugin, included in "Eclipse IDE for Java Devs"
  • 17.
  • 18.
  • 19.
    Want some more? •Maven books from Sonatype https://www.sonatype.com/ebooks • pom.xml reference • Maven Plugins reference o Apache o Codehaus

Editor's Notes

  • #10 "convention over configuration" guarantees success!
  • #12 First thing that maven downloads is itself! plugins, etc. ("Maven downloads the internet")
  • #13 Adding a Maven repo for the team to proxy central share internal artifacts between team members share releases with outsiders (until we're on central...)