Apache Maven for AT/QC

36
Apache Maven Volodymyr Ostapiv Aug 2014

description

Updated version of Apache Maven presentation

Transcript of Apache Maven for AT/QC

Page 1: Apache Maven for AT/QC

Apache Maven

Volodymyr OstapivAug 2014

Page 2: Apache Maven for AT/QC

Agenda

▪ What is Maven, Project lifecycle▪ Getting, installing, configuring Maven▪ POM, GAV, Archetype▪ pom.xml contents– dependencies– properties– exclusions– profiles

▪ Some useful plugins

Page 3: Apache Maven for AT/QC

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

Page 4: Apache Maven for AT/QC

Maven LifeCycle

•Default lifecycle– generate-sources/generate-resources– compile– test–package– integration-test (pre and post)– install–Deploy

•Separate “clean” lifecycle

Page 5: Apache Maven for AT/QC

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

Page 6: Apache Maven for AT/QC

Get it!

Page 7: Apache Maven for AT/QC

Configure

WindowsSettings: %MAVEN_HOME%\conf\settings.xmlRepository Location: UserProfile\.m2\

LinuxSettings: /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

Page 8: Apache Maven for AT/QC

Path Conventions

Page 9: Apache Maven for AT/QC

Create simple project

mvn archetype:generate -DgroupId=[myGroup]-DartifactId=[myArtifact] -DarchetypeArtifactId=maven-archetype-archetype

ORmvn archetype:generate

Page 10: Apache Maven for AT/QC

Arche-who? O_o

An archetype is defined as an original pattern or model from which all other things of the same kind are made.

Archetype is a Maven project templating toolkit.

Page 11: Apache Maven for AT/QC

Maven + IDE

mvn idea:idea mvn eclipse:eclipse

Generate project files for the most popular IDEs

Page 12: Apache Maven for AT/QC

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

Page 13: Apache Maven for AT/QC

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

Page 14: Apache Maven for AT/QC

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>

Page 15: Apache Maven for AT/QC

Dependencies

<dependencies><dependency>

<groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.5</version><scope>compile</scope>

</dependency></dependencies>

Page 16: Apache Maven for AT/QC

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)

Page 17: Apache Maven for AT/QC

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

Page 18: Apache Maven for AT/QC

Exclusions

<exclusions><exclusion>

<groupId>org.softserve.sse</groupId><artifactId>softserve-sse</artifactId>

</exclusion></exclusions>

Page 19: Apache Maven for AT/QC

Profiles

<profile><id>default</id><activation>

<activeByDefault>true</activeByDefault></activation>

</profile>

(mvn install -Pprofilename)

Page 20: Apache Maven for AT/QC

Profile activationOperating 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>

Page 21: Apache Maven for AT/QC

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>

Page 22: Apache Maven for AT/QC

Plugins

Here real magic starts

Page 23: Apache Maven for AT/QC

maven-surefire-plugin

surefire:test

Page 24: Apache Maven for AT/QC

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>

Page 25: Apache Maven for AT/QC

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>

Page 26: Apache Maven for AT/QC

jetty-maven-plugin

mvn jetty:run

Page 27: Apache Maven for AT/QC

tomcat-maven-plugin

mvn tomcat:run

Page 28: Apache Maven for AT/QC

maven-antrun-plugin

mvn antrun:run

Page 29: Apache Maven for AT/QC

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>

Page 30: Apache Maven for AT/QC

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>

Page 31: Apache Maven for AT/QC

maven-compiler-plugin

mvn compiler:compile mvn compiler:testCompile

Page 32: Apache Maven for AT/QC

maven-assembly-plugin

mvn assembly:single

Page 33: Apache Maven for AT/QC

maven-shade-plugin

mvn shade:shade

Page 34: Apache Maven for AT/QC

maven-dependency-plugin

mvn dependency:analyzemvn dependency:tree

Page 35: Apache Maven for AT/QC

selenium-maven-plugin

selenium:start-serverselenium:stop-serverselenium:seleneseselenium:xvfb

Page 36: Apache Maven for AT/QC

That’s all