Maven

Click here to load reader

download Maven

of 33

description

introduction to maven

Transcript of Maven

2. Preface
Do you ever begin a new project from scratch?
Whats you do before your first line code?
Do you maintain the build script in your Team?
Which kinds of tool do you use (make, ant, ivy,maven, mercury,)
3. Outline
Key concept
Basic Usage
Demo
20% usage
Ant vs Maven
4. Key concept
5. Plugin : (task in Ant)
Maven is a plugin execution framework.
Adding functionality to Maven is done through the plugin mechanism. (all work is done by plugins)
Similar to Ant Task/Ant Target(not exactly) from the user view
A collection of goals (tasks)
6. Plugin
7. Plugin
Ant
Maven








maven-compiler-plugin
2.1

1.6
1.6





8. Lifecycle : the plugin-goal (task) execution sequence
phases
goals( pluginname:goalname)
default


simple example build file



























9. Lifecycle
A lifecycle is made up of phases
These build phases are executed sequentially (including all of the prior steps)
A Build phase is made up of goals
A goal represents a specific task, minimum execute unit (task)
A goalis bound to zero (direct invocation) or more build phases
mvn jar:jar----------- plugin-goal prefix-name (mvngroupID:artifactID:version:goal)
mvn package ----------- lifecycle phase name ,would execute jar:jar, because jar:jar is one contained goal in package phase
10. Lifecycle: build-in lifecycle
clean
default
site
11. Lifecycle: default binding goals
12. Lifecycle : adding more goals by plugin
Plugin: are artifacts that provide goals (tasks) to Maven

org.apache.maven.plugins
maven-javadoc-plugin
2.7

doc
public
true
TNT-CAL API doc
TNT-CAL API doc
256m
src/main/java
utf8
en_us



api-doc
prepare-package

javadoc




13. Dependency: no flying jars
No need download dependency lib one by one, declare it, Maven download for you
Shrink the project size in svn



com.telenav
ace-client
1.0.0.15
release
jar


com.telenav
kernel
a1.3-b101
jar



14. Dependency: transitive
Compile ok, runtime NoClassDefFoundError ---- Need transitive jar
Transitive (Chain) A -> B -> C
Transitive config
Shortest path A -> B -> C -> D 2.0 and A -> E -> D 1.0
Dependency scope
Dependency management : inheritance; import
15. Dependency: transitive
Dependency scope (IVY configuration map)
C (compile)
B (compile)
A
D (runtime)
E (test)
compile
test
runtime
16. Profile
Different build configuration for different target environments
Different OS
Different deploy environment
Activation
-PCLI option
Based on environment variables
Maven will execute the steps in the profile in addition to the normal steps
17. Profile


cn-dev


target
cn-dev



gb2312



us-deploy


target
us-deploy



true



18. Basic Usage
19. Create a project
Create
mvn archetype:generate DarchetypeArtifactId=maven-archetype-quickstart DgroupId=com.telenav DartificatId=rgc
Project layout
http://stackoverflow.com/questions/4182794/in-maven-what-is-the-difference-between-main-resources-and-main-config
20. Demo : Hello World
Build
Package
Test
21. 20% usage
Eclipse plugin
Properties
Repository
Inheritance & Multiply-module
Write a maven-plugin
22. Eclipse-plugin m2eclipse
http://m2eclipse.sonatype.org/installing-m2eclipse.html
Add-dependency (search)
Add-plugin
23. Properties
Build in properties
${basedir} represents the directory containing pom.xml
${project.basedir} represents the directory containing pom.xml
Project properties
1.0 is accessible via ${project.version}.
Environment variables
${env.PATH}
Java System Properties
${file.separator}
User-defined Properties
Plugin property
expression: ${maven.compiler.target}
Setting property
24. Repository
Add repository
Distributionartifacts to release repository
Dependent on not published jar ?
mvn install:install-file
System scope (systemPath)
File Repository


telenav
http://tar1.telenav.com:8080/repository

true


false





corp1
Corporate Repository
scp://repo1/maven2default


mvn install:install-file -Dfile=
-DgroupId=
-DartifactId= -Dversion= -Dpackaging= -DgeneratePom=true
25. Inheritance & Multiply-module
Inheritance
Dependencies
Plugin lists/executions/configuration
Properties (Yes)
Profile is not inherited directly but activated
Multiply-Module (Aggregation, a single command:)

com.telenav
geoalert-masterpom
1.0.0


my-project
another-project

26. Maven-Plugin
Extends AbstractMojo
Project Definition
maven-plugin
Parameters : inject by Maven (IOC)
Annotation
/*extra files/folders to classpath
* @parameter
*/
protected File[]extraClassPathItems;


src/main/webapp


/**
* Location of class files
*
* @parameter expression="${project.build.outputDrectory}"
* @required
*/
private File outputDirectory;
27. ANT vs Maven
Declarative (Maven) and Imperative (Ant)
Convention and configuration (Maven) over configuration and scripting (Ant)
Ant
dist(Defaut)
generate-java
init
clean
compile

generate-resources
Maven
compile
compiler:compile
test
surefire:test
Standard project layout
pakcage
surefire:test
install
(Fix)Building Phases
Building Goals
28. Ant vs Maven
Maven
Good for Modularization
Dependency management
Not easy for beginner to understand
Bugs and issues are hard to track (understand the conventions)
Sometimes are slow
Ant
Easy to learn No so many abstraction
29. Reference
http://maven.apache.org/guides/index.html
http://www.sonatype.com/books/mvnex-book/reference/public-book.html