Mastering Maven 2.0 In 1 Hour V1.3

Post on 30-Aug-2014

26275 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation of Mastering Maven to the SDJUG team in San Diego, CA.

Transcript

by Matthew McCullough of Ambient Ideas, LLC

MavenMastering

Increase your MoJo-tivity

Matthew

http://refcardz.dzone.com/refcardz/apache-maven-2

Maven useon the upswing

Convention

Configurationover

DefaultGoal

Default Goal

‣ Often not set.‣ Saves typing.‣ Communicates author’s intended goal.‣ Only one goal or phase allowed.

mvn install

<project> <groupId>com.ambientideas</groupId> <artifactId>sample-defaultgoal</artifactId> [...] <build> <defaultGoal>install</defaultGoal> </build>

[...]</project>

mvn install

mvn install

mvn

SuperPom

Super Pom

‣ Pseudo-invisible.

Super Pom

‣ Pseudo-invisible.

‣ All projects inherit it.

Super Pom

‣ Pseudo-invisible.

‣ All projects inherit it.

‣ Specifies file location defaults.

Super Pom

‣ Pseudo-invisible.

‣ All projects inherit it.

‣ Specifies file location defaults.

‣ Locks version of common plugins.‣ post mvn 2.0.8.‣ Increases build stability.

<!-- START SNIPPET: superpom --><project> <modelVersion>4.0.0</modelVersion> <name>Maven Default Project</name>

<build> <directory>target</directory> <outputDirectory>target/classes</outputDirectory> <finalName>${project.artifactId}-${project.version}</finalName> [...] <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> </plugin> </plugins> </pluginManagement> </build></project>

Super Pom

‣ Familiarize yourself with it.‣ View it in SVN.

ArchetypesSaving time with

Project via Archetype

‣ Project templates on steroids.‣ Seed unit tests.‣ Standardize directory structure.‣ Corporate licenses, OSS licenses.‣ Bundle corporate READMEs.

‣ Doesn’t mutate like a copy-n-paste template.‣ Replaceables for classnames, company info.

$ mvn archetype:generate

Archetype via Project

‣ Uses existing project as seed.‣ Turns it into a maven archetype.‣ Can publish to a repo for others to use.

$ mvn archetype:create-from-project

Archetype via Archetype

‣ “Template for templates”

Archetype via Archetype

‣ “Template for templates”‣ Can be selected from the archetype list.

Archetype via Archetype

‣ “Template for templates”‣ Can be selected from the archetype list.

mvn archetype:generate

$ mvn archetype:generate -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeArtifactId=maven-archetype-archetype -DarchetypeVersion=1.0 -DgroupId=com.ambientideas -DartifactId=mysamplearchetype

DependencyManagement

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

JU:JU:3.8.2

JU:JU:3.8.1 B:B:2.0

Dir

ty T

ree

How does Maven resolve versions?

Nearest

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

JU:JU:3.8.2

JU:JU:3.8.1 B:B:2.0

Dir

ty T

ree

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

JU:JU:3.8.2

JU:JU:3.8.1 B:B:2.0

Dir

ty T

ree

Then highest version?

Then highest version?

Then sequential first at same distance

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

JU:JU:3.8.2

JU:JU:3.8.1 B:B:2.0

Dir

ty T

ree

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

JU:JU:3.8.2

JU:JU:3.8.1 B:B:2.0

Dir

ty T

ree

[INFO] [dependency:tree][INFO] root.project:ear:ear:1.0[INFO] +- root.project:ejbs:ejb:1.0:compile[INFO] | \- junit:junit:jar:4.0:compile[INFO] +- root.project.servlets:servlet:war:1.0:compile[INFO] +- root.project:primary-source:jar:1.0:compile[INFO] \- root.project.projects:logging:jar:1.0:compile

Firs

t se

quen

tial a

t sa

me

leve

l ➲

[INFO] [dependency:tree][INFO] root.project:ear:ear:1.0[INFO] +- root.project:ejbs:ejb:1.0:compile[INFO] | \- junit:junit:jar:4.0:compile[INFO] +- root.project.servlets:servlet:war:1.0:compile[INFO] +- root.project:primary-source:jar:1.0:compile[INFO] \- root.project.projects:logging:jar:1.0:compile

4.0 is resolved

Firs

t se

quen

tial a

t sa

me

leve

l ➲

[INFO] [dependency:tree][INFO] root.project:ear:ear:1.0[INFO] +- root.project:ejbs:ejb:1.0:compile[INFO] | \- junit:junit:jar:4.0:compile[INFO] +- root.project.servlets:servlet:war:1.0:compile[INFO] +- root.project:primary-source:jar:1.0:compile[INFO] \- root.project.projects:logging:jar:1.0:compile

Even though I have 4.4 declared as a dependency of primary-source

4.0 is resolved

Firs

t se

quen

tial a

t sa

me

leve

l ➲

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.1

JU:JU:3.8.2

JU:JU:3.8.2 B:B:2.0

Res

olvi

ng T

ree

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.1JU:JU:3.8.2 B:B:2.0

Res

olvi

ng T

ree

A:A:1.0

B:B:1.0 C:C:1.0

B:B:2.0

Res

olvi

ng T

ree

JU:JU:3.8.2

A:A:1.0

B:B:1.0 C:C:1.0

Res

olve

d Tr

ee

JU:JU:3.8.2

We’ve resolved the version tree.

How about the dependency graph?

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.1

JU:JU:3.8.2

JU:JU:3.8.2 B:B:2.0

Dir

ty G

raph

A:A:1.0

B:B:1.0 C:C:1.0

JU:JU:3.8.2

Res

olve

d G

raph

A dice rollfor complex graphs?

Declare desired versions

Advertise authorized versions

<*Management>

‣ <dependencyManagement>‣ Inheritance for dependency versions.

<*Management>

‣ <dependencyManagement>‣ Inheritance for dependency versions.

‣ <pluginManagement>‣ Inheritance for plugin versions and config.

<dependencyManagement> and <pluginManagement>

are essentially identical

Minimizing declarationswith

<*Management>

In the base pom.xml...

<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>echodir</id> <goals> <goal>run</goal> </goals> <phase>install</phase> <configuration> <tasks> <echo>Build Dir: ${project.build.directory}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins></pluginManagement>

<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>echodir</id> <goals> <goal>run</goal> </goals> <phase>install</phase> <configuration> <tasks> <echo>Build Dir: ${project.build.directory}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins></pluginManagement>

<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>echodir</id> <goals> <goal>run</goal> </goals> <phase>install</phase> <configuration> <tasks> <echo>Build Dir: ${project.build.directory}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins></pluginManagement>

<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>echodir</id> <goals> <goal>run</goal> </goals> <phase>install</phase> <configuration> <tasks> <echo>Build Dir: ${project.build.directory}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins></pluginManagement>

In the child pom.xml...

<plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> </plugin></plugins>

Dependency Lists

‣ List view‣ mvn dependency:resolve

‣ Tree view‣ mvn dependency:tree

‣ Plugin list view‣ mvn dependency:resolve-plugins

$ mvn dependency:tree[INFO] com.ambientideas:sample13-wicket:war:1.0-SNAPSHOT[INFO] +- org.apache.wicket:wicket:jar:1.3.2:compile[INFO] | \- org.slf4j:slf4j-api:jar:1.4.2:compile[INFO] +- org.apache.wicket:wicket-extensions:jar:1.3.2:compile[INFO] +- commons-collections:commons-collections:jar:3.1:compile[INFO] +- org.slf4j:slf4j-log4j12:jar:1.4.2:compile[INFO] +- log4j:log4j:jar:1.2.14:compile[INFO] +- junit:junit:jar:3.8.2:test[INFO] +- org.mortbay.jetty:jetty:jar:6.1.4:provided[INFO] | \- org.mortbay.jetty:servlet-api-2.5:jar:6.1.4:provided[INFO] +- org.mortbay.jetty:jetty-util:jar:6.1.4:provided[INFO] \- org.mortbay.jetty:jetty-management:jar:6.1.4:provided[INFO] +- mx4j:mx4j:jar:3.0.1:provided[INFO] \- mx4j:mx4j-tools:jar:3.0.1:provided

Dependency Analysis

‣ Analyze to help prune unneeded.‣ mvn dependency:analyze

Dependency Analysis

‣ See if overrides are colliding.‣ mvn dependency:analyze-dep-mgt

$ mvn dependency:analyze

[WARNING] Unused declared dependencies found:[WARNING] org.slf4j:slf4j-log4j12:jar:1.4.2:compile[WARNING] log4j:log4j:jar:1.2.14:compile[WARNING] org.mortbay.jetty:jetty-management:jar:6.1.4:provided[WARNING] org.apache.wicket:wicket-extensions:jar:1.3.2:compile[WARNING] commons-collections:commons-collections:jar:3.1:compile

DeBugging

Maven Debug Flags

‣ Output full error stacktraces.‣ mvn <anygoal> -e

‣ Output debug level operational info.‣ mvn <anygoal> -X‣ Always use when submitting questions or bug

reports.

$ mvn <anygoal> -X

[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-dependency-plugin:2.0:analyze' -->[DEBUG] (f) baseDir = /Users/mccm06/Documents/Teach/Courses/Mastering-Maven-1Hour/examples/maven-training.git/sample13-wicket-withdependencies[DEBUG] (f) failOnWarning = false[DEBUG] (f) project = MavenProject: com.ambientideas:sample13-wicket-withunneededdependencies:1.0-SNAPSHOT @ /Users/mccm06/Documents/Teach/Courses/Mastering-Maven-1Hour/examples/maven-training.git/sample13-wicket-withunneededdependencies/pom.xml[DEBUG] (f) scriptableFlag = $$%%%[DEBUG] (f) scriptableOutput = false[DEBUG] (f) verbose = false

Debug Maven

‣ Waits on socket for debugger to connect.‣ mvnDebug <anygoal>

‣ Useful for debugging plugins.

‣ Can be used for unit test debugging.‣ mvnDebug test -DforkMode=none

Debug Unit Tests

‣ Waits on socket for debugger to connect.

mvn test -Dmaven.surefire.debug

GroovySupport

Groovy Support‣ Groovy application code compilation.‣ mvn archetype:generate...gmaven-archetype-basic

‣ Groovy maven plugin authoring.‣ mvn archetype:generate...gmaven-archetype-mojo

‣ Joint compiler in both cases.

+

Grails Support‣ Grails now fully supports Maven...‣ Archetype‣ Grails goals: create-controller, run-app, etc.

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails \ -DarchetypeArtifactId=grails-maven-archetype \ -DarchetypeVersion=1.0-SNAPSHOT \ -DarchetypeRepository=http://snapshots.repository.codehaus.org \ -DgroupId=com.ambientideas -DartifactId=sample-grails

+

command lineHelp!

Maven Help Plugin

mvn help:describe -Dplugin=<anygoal>

‣ Lists and describes plugin goals.

Maven Help Plugin

mvn help:system

‣ Outputs environment variables and system properties.

Maven Help Plugin

mvn help:active-profiles

‣ See what profiles are being triggered by environment, files, params.

Maven Help Plugin

mvn help:effective-pom

‣ Outputs the resultant pom.‣ Includes inherited sections.

M2Eclipse

m2eclipse Plugin‣ Advanced Eclipse GUI for Maven‣ Update site:

http://m2eclipse.sonatype.org/update-dev/

‣ Eclipse 3.2-3.4 compatibility‣ 3.5 support arrived May 09

‣ Features include:‣ Exclusion/Inclusion via clicks‣ Searching for artifacts‣ Dependency diagrams‣ Click-to-run Maven goals

+

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

➊Outdated

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:m2eclipseFile > Import > Existing Projects into Workspace

Outdated

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:m2eclipseFile > Import > Existing Projects into Workspace

Outdated

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:m2eclipseFile > Import > Existing Projects into Workspace

Outdated

Unnecessary

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:m2eclipseFile > Import > Existing Projects into Workspace

File > Import > Maven Projects➌

Outdated

Unnecessary

mvn eclipse:eclipseFile > Import > Existing Projects into Workspace

Set up Eclipse Classpath Variable M2_REPO

mvn eclipse:m2eclipseFile > Import > Existing Projects into Workspace

File > Import > Maven Projects➌

Outdated

Unnecessary

Dependency Graph

Exclusions

Eclipse command line Compiler<plugin>     <artifactId>maven‐compiler‐plugin</artifactId>     <configuration>         <compilerId>eclipse</compilerId>         <source>1.5</source>         <target>1.6</target>     </configuration>     <dependencies>        <dependency>           <groupId>org.codehaus.plexus</groupId>           <artifactId>plexus‐compiler‐eclipse</artifactId>           <version>1.5.1</version>        </dependency>     </dependencies></plugin>

Source Code ControlIntegration

Maven SCM Support

‣ Seamlessly integrates Maven processes with your SCM repo.‣ mvn scm:diff‣ Creates UNIX diff file

‣ mvn scm:tag‣ mvn scm:status

Maven SCM Support

‣ Dozens of SCM systems supported.‣ CVS‣ Subversion‣ Git

http://docs.codehaus.org/display/SCM/SCM+Matrix

Maven SCM Support

mvn release:prepare

‣ Increments all pom.xml version tags.

ReactorPlugin

Reactor Plugin

Purposes‣ Spawns a new nested Maven build.‣ Restart where a long-running build failed.‣ Selective build based on dependency.‣ Selective build based on scm-noticed changes.

Reactor Plugin

In Use‣ Resume multi-module build starting with this

module.

$ mvn reactor:resume -Dfrom=mymodule

Reactor Plugin

In Use‣ Build all modules that are dependents of mine.‣ Essentially, selective children.‣ I just made a change.‣ Want to ensure I didn’t break anything below me.

$ mvn reactor:make-dependents -Dmake.folders=mid1 or$ mvn reactor:make-dependents -Dmake.projects=com.ambientideas:mid1

Reactor Plugin

In Use‣ Build all modules that this module depends on‣ Essentially, selective parents.‣ Teammate informed me.‣ Wants to make sure I test his changes.

$ mvn reactor:make -Dmake.folders=mid1 or$ mvn reactor:make -Dmake.projects=com.ambientideas:mid1

Reactor Plugin

In Use‣ Build all modules that scm says had changed.‣ Essentially, build “dirty”.

$ mvn reactor:make-scm-changes

RunningApps

Exec Plugin

‣ Run any Java main class or external tool.

Exec Plugin

‣ Run any Java main class or external tool.‣ Builds classpath from pom dependencies.

Exec Plugin

‣ Run any Java main class or external tool.‣ Builds classpath from pom dependencies.‣ Primarily for testing.

$ mvn exec:java -Dexec.mainClass="com.foo.Main"

$ mvn exec:java -Dexec.mainClass="com.foo.Main"

or

$ mvn exec:java -Dexec.mainClass="com.foo.Main"

or$ mvn exec:exec -Dexec.executable="winzip.exe" -Dexec.args="myfile.txt"

AntIntegration

Ant Build Generation

Why?‣ Dead simple.‣ Great for backport/legacy situations.

$ cd mymavenproj

$ cd mymavenproj

$ mvn ant:ant

$ cd mymavenproj

$ mvn ant:ant

$ lsbuild.xmlmaven-build.xmlmaven-build.properties

◀Yes, that’s it!

AntRun Plugin

‣ Run any Ant task.‣ Leverage legacy in-house plugins.‣ Great way to copy arbitrary files.‣ Useful for echoing status or properties.

<plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>echodir</id> <goals> <goal>run</goal> </goals> <phase>package</phase> <configuration> <tasks> <echo>BuildDir: ${project.build.directory}</echo> <copy todir="${myspecialfolder}"> <fileset dir="${myfiles}"/> </copy> </tasks> </configuration> </execution> </executions> </plugin></plugins>

SitesReportsand

Site Generation‣ Professional project web site

Site Generation‣ Professional project web site‣ Templatizable

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors‣ Licenses

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors‣ Licenses‣ Hyperlinked source ref

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors‣ Licenses‣ Hyperlinked source ref‣ Defect tracking

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors‣ Licenses‣ Hyperlinked source ref‣ Defect tracking‣ Dependencies

Site Generation‣ Professional project web site‣ Templatizable‣ Contributors‣ Licenses‣ Hyperlinked source ref‣ Defect tracking‣ Dependencies‣ Plugins

Project Info

Report Generation

‣ Unit test reports‣ Test coverage reports‣ Checkstyle and PMD‣ Javadocs‣ Tag list (TODO)

Report

What’s New?

Maven 2.0.10‣ Stability.‣ Unit and integration tests.‣ End of the line for 2.0.X.

Maven 2.1‣ Parallel downloads (Don Brown)‣ Encrypted passwords‣ Drop in replacement

What’s Next?

Maven 3.0‣ Encrypted passwords (from 2.1)‣ Mercury engine‣ Parallel downloads (determinate)‣ New resolver

‣ Tyco OSGi bundle builder

Build Nirvana

MatthewTwitter @matthewmccull

Blog http://www.ambientideas.com/blog sidebar has all my social media links

Email matthewm@ambientideas.com

GitHub http://github.com/matthewmccullough

Maven Homepagehttp://maven.apache.org/

Maven Sonatype eBookhttp://www.sonatype.com/book/

Matthew’s Maven Bookmarkshttp://delicious.com/matthew.mccullough/maven

GMaven Pluginhttp://groovy.codehaus.org/GMaven

Resources

Maven Superpomhttp://maven.apache.org/pom.html#The_Super_POM

Sonatype Central Repohttp://repository.sonatype.org/

Java.net Repohttp://download.java.net/maven/2/

Archetype Mini-Guidehttp://maven.apache.org/guides/mini/guide-creating-archetypes.html

Resources

GMaven Pluginhttp://groovy.codehaus.org/GMaven

Grails Maven Integrationhttp://www.grails.org/Maven+Integration

m2eclipse Plugin Homepagehttp://m2eclipse.codehaus.org/

m2eclipse Eclipse Update Sitehttp://m2eclipse.sonatype.org/update-dev/

m2eclipse Documentationhttp://docs.codehaus.org/display/MAVENUSER/Dealing+with+Eclipse-based+IDE

Resources

Archetype Guidehttp://maven.apache.org/guides/mini/guide-creating-archetypes.html

Sonatype eBook Archetype Chapterhttp://propellors.net/maven/book/archetypes.htmlhttp://www.sonatype.com/books/maven-book/reference/archetypes.html

Archetype create-from-project Mojohttp://maven.apache.org/plugins/maven-archetype-plugin/create-from-project-mojo.html

JavaWorld Reporting Articlehttp://www.javaworld.com/javaworld/jw-02-2006/jw-0227-maven.html

Resources

Maven Dependency Pluginhttp://maven.apache.org/plugins/maven-dependency-plugin/

Maven AntRun Pluginhttp://maven.apache.org/plugins/maven-antrun-plugin/

Maven Exec Pluginhttp://mojo.codehaus.org/exec-maven-plugin/usage.html

SuperPOM in SVNhttp://svn.apache.org/viewvc/maven/components/tags/maven-2.0.9/maven-project/src/main/resources/org/apache/maven/project/pom-4.0.0.xml?view=markup

Resources

‣ http://flickr.com/photos/chanchan222/2883910690/‣ http://flickr.com/photos/themarmot/3173506262/‣ http://commons.wikimedia.org/wiki/File:Napoleon4.jpg

Image Credits

top related