Apache Maven (3.0+)
Basic introduction
Dec.22.2013
Boy.Wang
Jar Hell & Plugin Hell
cosmos-saje
cosmos-lib saje
cosmos-core
xerces fscript fractal-adl dream-annot dream-core
antlr fractal-util julia-mixins perseus-cache
task-deployment julia-asm aval
fractal-rmi
easymock
developing project
intermediate depex
final depex
Main Purpose
1. Single project producing a single type.
2. Standard life-cycles and naming conventions.
3. Version and dependency management.
4. Repositories: several remote and one local, it means no
more externals or lib and no more jars in developer's
directories.
groupId artifactId version
Maven Working Flow
Maven
(core)
~/.m2/
repository
pom.xml
download
parse connect
plugins
libs artifacts
artifacts
use
The core of Maven consists of a very basic shell that knows only how to parse the command line, manage a
classpath, parse a POM file and download Maven plugins as needed.
Core Concepts
1. What is pom.xml?
2. What is Maven plugin and goal?
e.g. mvn archetype:generate -DgroupId=org.simple -DartifactId=simple
3. What is Maven lifecycle and phase?
e.g. mvn install
4. What is Maven coordinate?
5. What is Maven repositories and dependency?
6. What is Maven artifact?
mvn [options] [goal(s)] [phase(s)]
1. A Maven project is simply a folder that contains a pom.xml.
2. Every pom.xml implicitly inherits from Super POM.
http://maven.apache.org/ref/3.0.5/maven-model-builder/super-pom.html
pom.xml (Project Object Model)
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.demo</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
SAMPLE
Maven Plugin and Goal
1. A plugin is a collection of one or more goals.
a. Plugin can be written in Java, Ant or Ruby.
b. e.g. Jar plugin contains goals for creating JAR files.
c. e.g. Compiler plugin contains goals for compiling
source code and unit test.
2. A goal is a specific task that can be executed.
% mvn archetype:generate -DgroupId=xxx.yyy -DartifactId=simple
plugin's identifier goal's parameters
goal's identifier
Maven Lifecycle and Phase
1. Phase may have zero or more goals bound to it.
2. Lifecycle is the definition of an ordered list of phases which is
executed sequentially.
% mvn resources:resources 
compiler:compile 
resources:testResources 
compiler:testCompile 
surefire:test 
jar:jar 
install:install 
% mvn install
phase's identifier Is the same with...
Lifecycle is just like a lazy package that is made of Phases. It helps you to execute multiple Goals sequentially.
% mvn compile
generate-sources
compile
test-compile
test
package
install
deploy
Phase Plugin:Goal
ear:generate...
compiler:compile
compiler:testCompile
surefire:test
jar:jar
install:install
deploy:deploy
lifecycle:
generate-source -> compile
lifecycle:
generate-source -> compile -> ... -> deploy
% mvn deploy
Maven Coordinate
Maven coordinates define a set of identifiers which can be
used to uniquely identify a project.
<project ...>
...
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<groupId>org.eclipse.cdt</groupId>
<artifactId>cdt-parent</artifactId>
<packaging>jar</packaging>
<version>8.3.0-SNAPSHOT</version>
This packaging element affects the goals required by phases.
Maven Repositories and Depex
1. When you run Maven for the first time, you will notice that Maven
downloads a number of files from a remote Maven repository. In
Maven, artifacts and plugins are retrieved from a remote
repository when they are needed.
2. Maven uses the local repository to share dependencies across
local projects.
Maven publishes a JAR file and a slightly modified version of the project’s
pom.xml file in the local repository. Storing a POM file in the repository gives
other projects information about this project, most importantly what
dependencies it has.
% mvn install
Maven Artifact
An Maven artifact is a file that is produced by a build
execution and represents an application binary. e.g.
1. A project's library: JAR, WAR, EAR, ZIP, ....
2. Maven plugin: A JAR application to execute build
executions.
3. Maven archetype: A JAR application to create a Maven
project template.
4. Project metafile: pom.xml.
Summary
1. A goal is a "unit of work". It is a basic execution unit and
its behavior can be configured.
2. A phase is made up of goals and a lifecycle is made up of
phases.
3. Higher level of reusability between builds.
4. Maven is a build system not just a build tool.
Appendix
Reference
1. Free Books
a. http://www.sonatype.com/resources/books/maven-by-example
b. http://www.sonatype.com/resources/books/maven-the-complete-reference
2. Maven Lifecycle Reference
a. http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
b. http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-common-goals.html
3. Maven C/C++ Plugin
a. http://duns.github.io/maven-nar-plugin/

Note - Apache Maven Intro

  • 1.
    Apache Maven (3.0+) Basicintroduction Dec.22.2013 Boy.Wang
  • 2.
    Jar Hell &Plugin Hell cosmos-saje cosmos-lib saje cosmos-core xerces fscript fractal-adl dream-annot dream-core antlr fractal-util julia-mixins perseus-cache task-deployment julia-asm aval fractal-rmi easymock developing project intermediate depex final depex
  • 3.
    Main Purpose 1. Singleproject producing a single type. 2. Standard life-cycles and naming conventions. 3. Version and dependency management. 4. Repositories: several remote and one local, it means no more externals or lib and no more jars in developer's directories. groupId artifactId version
  • 4.
    Maven Working Flow Maven (core) ~/.m2/ repository pom.xml download parseconnect plugins libs artifacts artifacts use The core of Maven consists of a very basic shell that knows only how to parse the command line, manage a classpath, parse a POM file and download Maven plugins as needed.
  • 5.
    Core Concepts 1. Whatis pom.xml? 2. What is Maven plugin and goal? e.g. mvn archetype:generate -DgroupId=org.simple -DartifactId=simple 3. What is Maven lifecycle and phase? e.g. mvn install 4. What is Maven coordinate? 5. What is Maven repositories and dependency? 6. What is Maven artifact? mvn [options] [goal(s)] [phase(s)]
  • 6.
    1. A Mavenproject is simply a folder that contains a pom.xml. 2. Every pom.xml implicitly inherits from Super POM. http://maven.apache.org/ref/3.0.5/maven-model-builder/super-pom.html pom.xml (Project Object Model) <project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.demo</groupId> <artifactId>myapp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>myapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> SAMPLE
  • 7.
    Maven Plugin andGoal 1. A plugin is a collection of one or more goals. a. Plugin can be written in Java, Ant or Ruby. b. e.g. Jar plugin contains goals for creating JAR files. c. e.g. Compiler plugin contains goals for compiling source code and unit test. 2. A goal is a specific task that can be executed. % mvn archetype:generate -DgroupId=xxx.yyy -DartifactId=simple plugin's identifier goal's parameters goal's identifier
  • 8.
    Maven Lifecycle andPhase 1. Phase may have zero or more goals bound to it. 2. Lifecycle is the definition of an ordered list of phases which is executed sequentially. % mvn resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar install:install % mvn install phase's identifier Is the same with...
  • 9.
    Lifecycle is justlike a lazy package that is made of Phases. It helps you to execute multiple Goals sequentially. % mvn compile generate-sources compile test-compile test package install deploy Phase Plugin:Goal ear:generate... compiler:compile compiler:testCompile surefire:test jar:jar install:install deploy:deploy lifecycle: generate-source -> compile lifecycle: generate-source -> compile -> ... -> deploy % mvn deploy
  • 10.
    Maven Coordinate Maven coordinatesdefine a set of identifiers which can be used to uniquely identify a project. <project ...> ... ... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> <groupId>org.eclipse.cdt</groupId> <artifactId>cdt-parent</artifactId> <packaging>jar</packaging> <version>8.3.0-SNAPSHOT</version> This packaging element affects the goals required by phases.
  • 11.
    Maven Repositories andDepex 1. When you run Maven for the first time, you will notice that Maven downloads a number of files from a remote Maven repository. In Maven, artifacts and plugins are retrieved from a remote repository when they are needed. 2. Maven uses the local repository to share dependencies across local projects. Maven publishes a JAR file and a slightly modified version of the project’s pom.xml file in the local repository. Storing a POM file in the repository gives other projects information about this project, most importantly what dependencies it has. % mvn install
  • 12.
    Maven Artifact An Mavenartifact is a file that is produced by a build execution and represents an application binary. e.g. 1. A project's library: JAR, WAR, EAR, ZIP, .... 2. Maven plugin: A JAR application to execute build executions. 3. Maven archetype: A JAR application to create a Maven project template. 4. Project metafile: pom.xml.
  • 13.
    Summary 1. A goalis a "unit of work". It is a basic execution unit and its behavior can be configured. 2. A phase is made up of goals and a lifecycle is made up of phases. 3. Higher level of reusability between builds. 4. Maven is a build system not just a build tool.
  • 14.
  • 15.
    Reference 1. Free Books a.http://www.sonatype.com/resources/books/maven-by-example b. http://www.sonatype.com/resources/books/maven-the-complete-reference 2. Maven Lifecycle Reference a. http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference b. http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-common-goals.html 3. Maven C/C++ Plugin a. http://duns.github.io/maven-nar-plugin/