2. About Me
• Mert ÇALIŞKAN
• Software Plumber :)
SCJP certified bla bla..
• Open Source Software advocate.
Involved in numerous OSS projects like
PrimeFaces, mesir ...
• Apache Software Foundation member
OpenLogic Expert Community member
http://www.jroller.com/mert
http://www.twitter.com/mertcaliskan
Java Developer Conference 2010
3. About You
+
Gradle
Java Developer Conference 2010
4. Agenda
• Maven is ? • IDE Support
• Core Concepts • How to increase
- The POM your MOJOtivity ?
- Build Life Cycle
- Dependency Mechanism
- Repositories
- Plugins
• Archetypes
Java Developer Conference 2010
6. Maven is...
• A project management framework from ASF
• Uniform build system
• Making the build as easy as possible
• Comprehensive model for software projects
• Versions:
1.x
2.x ( 2.0.x, 2.2.x(jdk1.5) )
3.x
2.x projects & plugins are still supported
completely rewritten - new artifact resolution system / dependency resolving
algorithm. Better IDE support
Java Developer Conference 2010
7. Highlights
• Convention over Configuration
• Common Interfaces
The time of build engineers is over
Stop building the build and focus on development !
• Dependency Management
Public repositories
• Plugin Architecture
• Documentation
Generate documentation, reports ...
Java Developer Conference 2010
9. The POM
• It is what makes your project a maven project
• An XML file
• Not just for building the project
project’s relationship, list of team members, license, SCM and etc.
• Not only for Java projects
build FLEX code with appropriate plugins
build Microsoft binaries from C# code
Java Developer Conference 2010
12. Simplest POM
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.egjug.jdc2010</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
</project>
}
compile code
run tests
$ mvn install package as jar
deploy to local repo
Java Developer Conference 2010
13. How is that possible with 6 lines of XML?
Where do I define the source folders, test
folders and all other stuff ?
Java Developer Conference 2010
14. Super POM
• Same analogy with java.lang.Object
• ${M2_HOME}/lib/ maven-2.2.1-uber.jar!
/org/apache/maven/project/pom-4.0.0.xml
• Standard directory layout
• Locks versions of common plugins >= 2.0.9
• Repo def. for http://repo1.maven.org/maven2
• To see the merged POM:
$!mvn help:effective-pom
Java Developer Conference 2010
15. BUILD
LIFE CYCLE
Java Developer Conference 2010
16. Build Life Cycle
• Process for building and distributing an artifact is clearly
defined
• 3 built-in Life Cycles
default: handle project deployment
clean: clean project files generated by a build
site: generate project’s site doc.
• Life Cycle consists of phases in specific order
• Zero or more goals attached to each phase
Java Developer Conference 2010
20. Dependency Mechanism
• Gimme the Coordinates ! (GAV factor)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>compile</scope>
</dependency>
• Transitive Dependencies
You can exclude what you don’t want
or omit them all with <optional>false</optional>
Java Developer Conference 2010
23. Scopes for Dependencies
compile
provided
runtime
test
system
import
CTR
Classpaths
Compile Test Runtime
Java Developer Conference 2010
24. CTR compile default scope, compile scoped dependencies will be in classpath
similar to the compile, artifact should be provided by
CT provided JDK / container @ runtime
TR runtime not needed for compilation but needed @ runtime
T test only needed for test compilation & execution
same as provided, but artifact should be provided
CT system explicitly with <systemPath />
with maven 2.0.9...dependency to be replaced with
--- import the dependencies in that POM's
<dependencyManagement> section.
Java Developer Conference 2010
25. Versioning and Ranges
<major>.<minor>.<incremental> - <qualifier>
1.2.3 / 1.2.3-alpha-01
Be careful with string comparison, ordering at the qualifiers...
1.2.3-alpha-2 > 1.2.3-alpha-10
(, ) - Exclusive
[, ] - Inclusive <version>[3.8,4.0)</version>
Java Developer Conference 2010
26. SNAPSHOT qualifier
• Active development version
• Resolve from remote Repo:
myjar-1.0.0-SNAPSHOT.jar
applies pattern date . time - build#
resolves to myjar-1.0.0-20100219.141012-1.jar
Install to local Repo:
1.0.0-SNAPSHOT will be 1.0.0-SNAPSHOT
Java Developer Conference 2010
27. How Maven
Resolve
Versions?
Java Developer Conference 2010
28. Compile
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging commons-logging commons-logging
commons-logging
B : B : 2.0 commons-logging commons-logging
1.0.4 1.1.1 1.0.1
commons-logging
commons-logging
1.1.1
Java Developer Conference 2010
Dependency Graph
29. A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
Which B will Maven choose?
commons-logging commons-logging commons-logging
commons-logging
B : B : 2.0 commons-logging commons-logging
1.0.4 1.1.1 1.0.1
commons-logging
commons-logging
1.1.1
B : 2.0 ?
Highest One?
Java Developer Conference 2010
30. Closest One
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging commons-logging commons-logging
commons-logging
B : B : 2.0 commons-logging commons-logging
1.0.4 1.1.1 1.0.1
commons-logging
commons-logging
1.1.1
Java Developer Conference 2010
31. Which commons-logging?
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
?
commons-logging
commons-logging
1.0.4
B : B : 2.0 commons-logging
commons-logging
1.1.1 ?
commons-logging
commons-logging
1.0.1 ?
?
commons-logging
commons-logging
1.1.1
Java Developer Conference 2010
32. The sequential first at the same distance
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging commons-logging commons-logging
commons-logging
B : B : 2.0 commons-logging commons-logging
1.0.4 1.1.1 1.0.1
commons-logging
commons-logging
1.1.1
Java Developer Conference 2010
33. A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging
commons-logging
1.0.4
Dependency Graph
Java Developer Conference 2010
34. IS PROJECT A GONNA WORK ?
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging
commons-logging
1.0.4
Dependency Graph
Java Developer Conference 2010
35. A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging
commons-logging
1.0.4
Dependency Graph
Java Developer Conference 2010
36. USE <dependencyManagement />
A : A : 1.0
D : D : 1.0 C : C : 1.0 B : B : 1.0
commons-logging commons-logging commons-logging
commons-logging
B : B : 2.0 commons-logging commons-logging
1.0.4 1.1.1 1.0.1
commons-logging
commons-logging
1.1.1
Java Developer Conference 2010
Dependency Graph
48. Repositories
• Repositories are for managing build artifacts and
dependencies
• local or remote repositories
• Can store jar, war, ear, ejb, rar ....
• Maven looks to at least 2 repository if none
specified
- local one
- http://repo1.maven.org/maven2 /uber-pom/
• Release & Snapshot repositories
Java Developer Conference 2010
50. Create Your own Repo
• Go Corporate !
• Deploy and share your projects artifacts
across your team
• Cache dependencies
Decrease the internet dependency
• Proxy Repo configurations
• OSS Alternatives:
Nexus
Archiva
Artifactory
Java Developer Conference 2010
51. Sonatype Nexus
Nexus Repository Manager
• Just a Web App, nice UI
• Upload artifacts via HTTP, RESTful
style
• Indexing and searching for repository
artifacts
• LDAP Integration with v1.5
Nexus Professional ($$$)
- Staging repository
- Eclipse plugin repository and more...
Java Developer Conference 2010
54. Plugins
• Plugin-oriented Architecture
plugin for even compiling the code..
• A maven artifact w/ descriptor (plugin.xml) and one
or more MOJOs
• Plexus as its IoC
Why not Spring?
• Where to look for plugins:
org.apache.maven.plugins
org.codehaus.mojo
Java Developer Conference 2010
55. Plugins available @ maven
Packaging Integration
jar war ear ejb
Core rar pom shade Eclipse
clean IDEA
compiler
deploy
failsafe
install Maven Tools Ant
resources Reporting
Repo Antlr
site Antrun
Changelog
surefire Archetype
Changes
verifier Assembly
Checkstyle
Clover Others Cargo Help
Javadocs jaxme Release
PMD jetty
Surefire-reports tomcat
Java Developer Conference 2010
59. Archetypes from Existing Project
• Use an existing project as a seed for archetype
$ mvn archetype:create-from-project
• generated archetype under :
target / generated-sources / archetype
Java Developer Conference 2010
61. Maven with IDEs
• Eclipse 3.5 Galileo
Eclipse IAM (formerly Q4E)
m2eclipse
maven-eclipse-plugin @ central-repo
• NetBeans 6.8
out of the box - embedded maven
• Intellij IDEA 9
out of the box - embedded maven
maven-idea-plugin @ central-repo
Java Developer Conference 2010
62. m2eclipse
• Eclipse plug-in from Sonatype
• Checkout maven project from Source Control
• Create new Project from Archetype
• Nice POM Editor
• Embedded Maven Console
• You might need subclipse & AJDT plugins.
• A quick Demo...
Java Developer Conference 2010
63. INCREASE
YOUR
MOJOTIVITY
Java Developer Conference 2010
64. Maven Help Plugin
mvn help:describe -Dplugin=<anygoal>
• Lists & describes the goals of the plugin
mvn help:system
• Lists system properties and env. variables
Java Developer Conference 2010
65. Use Profiles
• Create profiles for : development, staging and production
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>false</debug>
<optimize>true</optimize>
</configuration> <profiles>
</plugin> <profile>
</plugins>
<activation>
</build>
</profile> <jdk>[1.3,1.6)</jdk>
</activation>
...
• invoke like </profile>
$ mvn <phase> -Pproduction
Java Developer Conference 2010
66. Property References
env.*
environment variables exposed by OS or Shell
${env.JAVA_HOME}
project.*
POM elements
${project.groupId} ${project.parent.groupId}
settings.*
Maven settings.xml properties
${settings.localRepository}
${foo} <properties>
<foo>bar</foo>
</properties>
Java Developer Conference 2010
67. Debugging Maven
• Output full error stacktrace
mvn <anygoal> -e
• Output for debugging
mvn <anygoal> -X
Always use when submitting questions or bug reports.
Java Developer Conference 2010
68. GOOD PRACTICES
• If you are starting from scratch, start with MAVEN..!
• A Repository Manager is a MUST
Have a Corporate Repo
• Have a company specific POM, try to keep your POM
simple
portable avoid any hardcoding..
• Use profiles...
• Use <dependencyManagement />
coerce Maven to use a particular version
enforce consistency within projects
Java Developer Conference 2010
69. GOOD PRACTICES
• Make it so that you can,
Release early, Release Often
- Quick, easy and reproducible releases
- CI tool integration
• Each project should output its own artifact (SoC)
• Use IDE integrations
from stone age to iron age...
•Do not ignore or skip your tests!
-Dmaven.test.skip=true
-Dmaven.test.failure.ignore=true
Java Developer Conference 2010
70. Online Resources
http://www.sonatype.com/book
Maven: The Complete Reference
Maven By Example
m2eclipse Book
Nexus Book
http://www.maestrodev.com/better-build-maven
Better Builds with Maven
Java Developer Conference 2010