Download - Apache Maven for SoftServe IT Academy
Apache Maven
Volodymyr Ostapiv2014
Agenda
▪ What is Maven▪ Getting and installing▪ Basics: POM, GAV, Archetype▪ pom.xml contents▪ Some useful plugins
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
%MAVEN_HOME%\conf\settings.xml
/usr/local/maven/conf/settings.xml
UserProfile\.m2\
~/.m2/
Create simple project
mvn archetype:generate -DgroupId=[myGroup]-DartifactId=[myArtifact] -DarchetypeArtifactId=maven-archetype-archetype
ORmvn archetype:generate
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.
Maven + IDE
mvn idea:idea mvn eclipse:eclipse
Generate project files for the most popular IDEs
Path Conventions
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• 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 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>
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
jetty-maven-plugin
mvn jetty:run
tomcat-maven-plugin
mvn tomcat:run
maven-antrun-plugin
mvn antrun:run
maven-compiler-plugin
mvn compiler:compile mvn compiler:testCompile
maven-assembly-plugin
mvn assembly:single
maven-shade-plugin
mvn shade:shade
maven-dependency-plugin
mvn dependency:analyzemvn dependency:tree
That’s all