KEMBAR78
Apache Maven basics | PPTX
Apache Maven 
Volodymyr Ostapiv 
Aug 2014
Agenda 
▪ What is Maven, Project lifecycle 
▪ Getting, installing, configuring Maven 
▪ POM, GAV, Archetype 
▪ pom.xml contents 
– dependencies 
– properties 
– exclusions 
– profiles 
▪ Some useful plugins 
▪ Creating executable JAR
What is Maven 
▪Maven is a build automation tool used 
primarily for Java projects. 
▪ Maven addresses two aspects of building 
software: 
1. it describes how software is built 
2. it describes its dependencies
Maven LifeCycle 
•Default lifecycle 
–generate-sources/generate-resources 
–compile 
–test 
–package 
–integration-test (pre and post) 
–install 
–Deploy 
•Separate “clean” lifecycle
History 
– Maven 1 (2003) 
– Maven 2 (2005) 
▪ Not backwards Compatible 
– Maven 3 (2010) 
▪ Same as Maven 2 but more stable and with some 
additional features
Get it!
Configure 
Windows 
Settings: %MAVEN_HOME%confsettings.xml 
Repository Location: UserProfile.m2 
Linux 
Settings: /usr/local/maven/conf/settings.xml 
Repository Location: ~/.m2/ 
Use your own temporary settings for maven: 
mvn --settings=[PATH_TO_SETTINGS_FILE] 
Adding a Local repository location: 
mvn -Dmaven.repo.local=/path/to/local/repo
Path Conventions
Create simple project 
mvn archetype:generate 
-DgroupId=[myGroup] 
-DartifactId=[myArtifact] 
-DarchetypeArtifactId=maven-archetype- 
archetype 
OR 
mvn archetype:generate
Arche-who? O_o 
Archetype is a Maven project templating toolkit. 
An archetype is defined as an original pattern or model 
from which all other things of the same kind are made.
Maven + IDE 
Generate project files for the most popular IDEs 
mvn idea:idea mvn eclipse:eclipse
Maven Project Object Model (POM) 
▪ Describes a project 
– Name and Version 
– Artifact Type 
– Source Code Locations 
– Dependencies 
– Plugins 
– Profiles (Alternate build config.) 
▪ Uses XML by default
Project Name (GAV) 
▪ Maven uniquely identifies a project using: 
– groupID: project grouping identifier (no spaces 
or colons) 
▪ Usually loosely based on Java package 
– artfiactId: name of project (no spaces or 
colons) 
– version: Version of project 
▪ Format {Major}.{Minor}.{Maintanence} 
▪ Add ‘-SNAPSHOT ‘ to identify in development
Project Name (GAV) 
<?xml version="1.0" encoding="UTF-8"?> 
<project> 
<modelVersion>4.0.0</modelVersion> 
<groupId>org.lds.training</groupId> 
<artifactId>maven-training</artifactId> 
<version>1.0</version> 
<name>Windows 8</name> 
<description>The best OS ever!</description> 
<packaging>jar</packaging> 
<properties> 
<java.version>1.6</java.version> 
<properties> 
</project>
Dependencies 
<dependencies> 
<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>slf4j-simple</artifactId> 
<version>1.7.5</version> 
<scope>compile</scope> 
</dependency> 
</dependencies>
Dependency scope 
• compile (default) 
• provided (by JDK or a container at runtime) 
• runtime (not required for compilation) 
• test (used only during tests) 
• system (provided locally) 
• import (only available in Maven 2.0.9 or later)
Properties 
<properties> 
<jdk.version>1.6</jdk.version> 
<spring.version>3.1.2.RELEASE</spring.version> 
<spring.batch.version>2.1.8.RELEASE</spring.batch.version> 
</properties> 
<dependency> 
<groupId>org.springframework.batch</groupId> 
<artifactId>spring-batch-infrastructure</artifactId> 
<version>${spring.batch.version}</version> 
</dependency> 
mvn install -Dmyproperty=my property from command line
Exclusions 
<exclusions> 
<exclusion> 
<groupId>org.softserve.sse</groupId> 
<artifactId>softserve-sse</artifactId> 
</exclusion> 
</exclusions>
Profiles 
<profile> 
<id>default</id> 
<activation> 
<activeByDefault>true</activeByDefault> 
</activation> 
</profile> 
(mvn install -Pprofilename)
Profile activation 
Operating System based: 
<activation> 
<os> 
<family>unix</family> 
</os> 
</activation> 
<activation> 
<os> 
<name>Windows XP</name> 
<family>Windows</family> 
<arch>x86</arch> 
<version>5.1.2600</version> 
</os> 
</activation> 
JDK based: 
<activation> 
<jdk>1.4</jdk> 
</activation> 
<activation> 
<jdk>[1.3,1.6)</jdk> 
</activation>
Profile activation 
System property based: 
<activation> 
<property> 
<name>environment</name> 
<value>test</value> 
</property> 
</activation> 
File system state based: 
<activation> 
<file> 
<missing> 
target/maven/somefile 
</missing> 
</file> 
</activation>
Plugins 
Here real magic starts
maven-surefire-plugin 
surefire:test
Surefire: configuration 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-surefire-plugin</artifactId> 
<version>2.16</version> 
<configuration> 
All stuff goes here 
</configuration> 
</plugin>
Surefire: execution 
<skip>false</skip> 
<includes> 
<include>**/*Test.java</include> 
</includes> 
<excludes> 
<exclude>**/TestToSkip.java</exclude> 
</excludes> 
<!-- parallel execution --> 
<parallel>methods</parallel> 
<threadCount>10</threadCount> 
<useUnlimitedThreads>false</useUnlimitedThreads> 
<forkCount>4</forkCount> 
<reuseForks>true</reuseForks>
jetty-maven-plugin 
mvn jetty:run
tomcat-maven-plugin 
mvn tomcat:run
maven-antrun-plugin 
mvn antrun:run
maven-antrun-plugin 
<plugin> 
<artifactId>maven-antrun-plugin</artifactId> 
<version>1.7</version> 
<executions> 
<execution> 
<phase>generate-sources</phase> 
<configuration> 
<tasks> 
<!-- Place any ant task here (<target> </target> in a build.xml) --> 
</tasks> 
</configuration> 
<goals> 
<goal>run</goal> 
</goals> 
</execution> 
</executions> 
</plugin>
maven-antrun-plugin 
<tasks> 
<exec dir="${project.basedir}" 
executable="${project.basedir}/src/main/sh/doit.sh" failonerror="true"> 
<arg line="arg1 arg2 arg3 arg4" /> 
</exec> 
</tasks> 
<tasks> 
<copy todir="/builds/app/${project.version}"> 
<fileset dir="${project.build.directory}/rpm"/> 
</copy> 
<delete file="src/main/resources/db_conf.properties" /> 
</tasks> 
<tasks> 
<chmod file="src/main/resources/run.sh " perm="755" /> 
</tasks>
maven-compiler-plugin 
mvn compiler:compile 
mvn compiler:testCompile
maven-dependency-plugin 
mvn dependency:analyze 
mvn dependency:tree
selenium-maven-plugin 
selenium:start-server 
selenium:stop-server 
selenium:selenese 
selenium:xvfb
Creating executable JAR 
▪maven-jar-plugin 
(uses classpath 
dependencies) 
▪maven-assembly-plugin 
(all-in-one file) 
▪maven-shade-plugin 
(all-in-one file)
maven-jar-plugin 
mvn jar:jar
maven-jar-plugin 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-jar-plugin</artifactId> 
<version>2.4</version> 
<configuration> 
<archive> 
<manifest> 
<mainClass>org.akceptor.Main</mainClass> 
</manifest> 
</archive> 
</configuration> 
</plugin>
onejar-maven-plugin 
mvn onejar:one-jar
maven-assembly-plugin 
mvn assembly:single
maven-shade-plugin 
mvn shade:shade
Configurations and executions 
<executions> 
<execution> 
<id>execution1</id> 
<phase>test</phase> 
<configuration> 
<timeout>10</timeout> 
<options> 
<option>one</option> 
<option>two</option> 
</options> 
</configuration> 
<goals> 
<goal>doit</goal> 
</goals> 
</execution> 
</executions>
That’s all

Apache Maven basics

  • 1.
    Apache Maven VolodymyrOstapiv Aug 2014
  • 2.
    Agenda ▪ Whatis Maven, Project lifecycle ▪ Getting, installing, configuring Maven ▪ POM, GAV, Archetype ▪ pom.xml contents – dependencies – properties – exclusions – profiles ▪ Some useful plugins ▪ Creating executable JAR
  • 3.
    What is Maven ▪Maven is a build automation tool used primarily for Java projects. ▪ Maven addresses two aspects of building software: 1. it describes how software is built 2. it describes its dependencies
  • 4.
    Maven LifeCycle •Defaultlifecycle –generate-sources/generate-resources –compile –test –package –integration-test (pre and post) –install –Deploy •Separate “clean” lifecycle
  • 5.
    History – Maven1 (2003) – Maven 2 (2005) ▪ Not backwards Compatible – Maven 3 (2010) ▪ Same as Maven 2 but more stable and with some additional features
  • 6.
  • 7.
    Configure Windows Settings:%MAVEN_HOME%confsettings.xml Repository Location: UserProfile.m2 Linux Settings: /usr/local/maven/conf/settings.xml Repository Location: ~/.m2/ Use your own temporary settings for maven: mvn --settings=[PATH_TO_SETTINGS_FILE] Adding a Local repository location: mvn -Dmaven.repo.local=/path/to/local/repo
  • 8.
  • 9.
    Create simple project mvn archetype:generate -DgroupId=[myGroup] -DartifactId=[myArtifact] -DarchetypeArtifactId=maven-archetype- archetype OR mvn archetype:generate
  • 10.
    Arche-who? O_o Archetypeis a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.
  • 11.
    Maven + IDE Generate project files for the most popular IDEs mvn idea:idea mvn eclipse:eclipse
  • 12.
    Maven Project ObjectModel (POM) ▪ Describes a project – Name and Version – Artifact Type – Source Code Locations – Dependencies – Plugins – Profiles (Alternate build config.) ▪ Uses XML by default
  • 13.
    Project Name (GAV) ▪ Maven uniquely identifies a project using: – groupID: project grouping identifier (no spaces or colons) ▪ Usually loosely based on Java package – artfiactId: name of project (no spaces or colons) – version: Version of project ▪ Format {Major}.{Minor}.{Maintanence} ▪ Add ‘-SNAPSHOT ‘ to identify in development
  • 14.
    Project Name (GAV) <?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>org.lds.training</groupId> <artifactId>maven-training</artifactId> <version>1.0</version> <name>Windows 8</name> <description>The best OS ever!</description> <packaging>jar</packaging> <properties> <java.version>1.6</java.version> <properties> </project>
  • 15.
    Dependencies <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.5</version> <scope>compile</scope> </dependency> </dependencies>
  • 16.
    Dependency scope •compile (default) • provided (by JDK or a container at runtime) • runtime (not required for compilation) • test (used only during tests) • system (provided locally) • import (only available in Maven 2.0.9 or later)
  • 17.
    Properties <properties> <jdk.version>1.6</jdk.version> <spring.version>3.1.2.RELEASE</spring.version> <spring.batch.version>2.1.8.RELEASE</spring.batch.version> </properties> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> mvn install -Dmyproperty=my property from command line
  • 18.
    Exclusions <exclusions> <exclusion> <groupId>org.softserve.sse</groupId> <artifactId>softserve-sse</artifactId> </exclusion> </exclusions>
  • 19.
    Profiles <profile> <id>default</id> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> (mvn install -Pprofilename)
  • 20.
    Profile activation OperatingSystem based: <activation> <os> <family>unix</family> </os> </activation> <activation> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> JDK based: <activation> <jdk>1.4</jdk> </activation> <activation> <jdk>[1.3,1.6)</jdk> </activation>
  • 21.
    Profile activation Systemproperty based: <activation> <property> <name>environment</name> <value>test</value> </property> </activation> File system state based: <activation> <file> <missing> target/maven/somefile </missing> </file> </activation>
  • 22.
    Plugins Here realmagic starts
  • 23.
  • 24.
    Surefire: configuration <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> All stuff goes here </configuration> </plugin>
  • 25.
    Surefire: execution <skip>false</skip> <includes> <include>**/*Test.java</include> </includes> <excludes> <exclude>**/TestToSkip.java</exclude> </excludes> <!-- parallel execution --> <parallel>methods</parallel> <threadCount>10</threadCount> <useUnlimitedThreads>false</useUnlimitedThreads> <forkCount>4</forkCount> <reuseForks>true</reuseForks>
  • 26.
  • 27.
  • 28.
  • 29.
    maven-antrun-plugin <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>generate-sources</phase> <configuration> <tasks> <!-- Place any ant task here (<target> </target> in a build.xml) --> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
  • 30.
    maven-antrun-plugin <tasks> <execdir="${project.basedir}" executable="${project.basedir}/src/main/sh/doit.sh" failonerror="true"> <arg line="arg1 arg2 arg3 arg4" /> </exec> </tasks> <tasks> <copy todir="/builds/app/${project.version}"> <fileset dir="${project.build.directory}/rpm"/> </copy> <delete file="src/main/resources/db_conf.properties" /> </tasks> <tasks> <chmod file="src/main/resources/run.sh " perm="755" /> </tasks>
  • 31.
  • 32.
  • 33.
  • 34.
    Creating executable JAR ▪maven-jar-plugin (uses classpath dependencies) ▪maven-assembly-plugin (all-in-one file) ▪maven-shade-plugin (all-in-one file)
  • 35.
  • 36.
    maven-jar-plugin <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass>org.akceptor.Main</mainClass> </manifest> </archive> </configuration> </plugin>
  • 37.
  • 38.
  • 39.
  • 40.
    Configurations and executions <executions> <execution> <id>execution1</id> <phase>test</phase> <configuration> <timeout>10</timeout> <options> <option>one</option> <option>two</option> </options> </configuration> <goals> <goal>doit</goal> </goals> </execution> </executions>
  • 41.

Editor's Notes

  • #19 <dependency> <groupId>net.lightbody.bmp</groupId> <artifactId>browsermob-proxy</artifactId> <version>2.0-beta-8</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.softserve.sse</groupId> <artifactId>softserve-sse</artifactId> </exclusion> </exclusions> </dependency>
  • #20 <profiles> <profile> <id>default</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <finalName>${name}</finalName> <plugins> <plugin>…</plugin> <plugins> </build> </profile> </profiles>
  • #21 <activation> <os> <family>windows</family> </os> </activation>
  • #23 Maven is – at its heart – a plugin execution framework. All work is done by plugins. Looking for a specific goal to execute. There are the build and the reporting plugins: Build plugins will be executed during the build and they should be configured in the <build/> element from the POM. Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element from the POM.
  • #24 http://maven.apache.org/surefire/maven-surefire-plugin/ Maven Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in 2 different file formats: Plain text files (*.txt) XML files (*.xml) By default, these files are generated at ${basedir}/target/surefire-reports. For an HTML format of the report, please see the Maven Surefire Report Plugin.
  • #25 mvn –DskipTests mvn -Dmaven.test.skip=true
  • #26 Parallel execution * parallel: methods, classes, both, suites, suitesAndClasses, suitesAndMethods, classesAndMethods, all ** threadCount: <threadCountMethods>10</threadCountMethods> <threadCountClasses>10</threadCountClasses> <threadCountSuites>10</threadCountSuites>
  • #27 http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
  • #28 http://tomcat.apache.org/maven-plugin.html
  • #29 https://maven.apache.org/plugins/maven-antrun-plugin/
  • #30 https://maven.apache.org/plugins/maven-antrun-plugin/
  • #31 https://maven.apache.org/plugins/maven-antrun-plugin/
  • #32 <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-proc:none</compilerArgument> </configuration> <executions> <execution> <id>run-annotation-processors-only</id> <phase>generate-sources</phase> <configuration> <compilerArgument>-proc:only</compilerArgument> <!-- If your app has multiple packages, use this include filter to execute the processor only on the package containing your entities --> <!-- <includes> <include>**/model/*.java</include> </includes> --> </configuration> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>
  • #33 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>analyze</id> <phase>verify</phase> <goals> <goal>analyze-only</goal> </goals> <configuration> <failOnWarning>true</failOnWarning> </configuration> </execution> </executions> </plugin>
  • #34 http://mojo.codehaus.org/selenium-maven-plugin/ <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>selenium-maven-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>pre-integration-test</phase> <goals> <goal>start-server</goal> </goals> <configuration> <background>true</background> </configuration> </execution> </executions> </plugin>
  • #37 For Spring apps use onejar-maven-plugin <plugin> <groupId>com.jolira</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.4</version> <executions> <execution> <configuration> <mainClass>com.package.core.Main</mainClass> <!-- Optional, default is false --> <attachToBuild>true</attachToBuild> <!-- Optional, default is "one jar" --> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin>
  • #38 For Spring apps use onejar-maven-plugin <plugin> <groupId>com.jolira</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.4</version> <executions> <execution> <configuration> <mainClass>com.package.core.Main</mainClass> <!-- Optional, default is false --> <attachToBuild>true</attachToBuild> <!-- Optional, default is "one jar" --> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin> http://onejar-maven-plugin.googlecode.com/svn/mavensite/usage.html
  • #39 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.akceptor.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
  • #40 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.7.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <finalName>${project.artifactId}</finalName> <outputDirectory> ${project.build.directory}/${project.artifactId}/bin </outputDirectory> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.akceptor.Main</mainClass> </transformer> </transformers> </configuration> </plugin> http://maven.apache.org/plugins/maven-shade-plugin/
  • #41 http://maven.apache.org/guides/mini/guide-configuring-plugins.html