• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mastering Maven 2.0 In 1 Hour V1.3
 

Mastering Maven 2.0 In 1 Hour V1.3

on

  • 26,202 views

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

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

Statistics

Views

Total Views
26,202
Views on SlideShare
26,167
Embed Views
35

Actions

Likes
23
Downloads
845
Comments
0

4 Embeds 35

http://www.slideshare.net 25
https://wiki.thomsonreuters.com 7
https://thehub.thomsonreuters.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Mastering Maven 2.0 In 1 Hour V1.3 Mastering Maven 2.0 In 1 Hour V1.3 Presentation Transcript

    • Mastering Maven ncrease your Mo Jo-tivity I by Matthew McCullough of Ambient Ideas, LLC
    • Matthew
    • http://refcardz.dzone.com/refcardz/apache-maven-2
    • Maven use ing up on the sw
    • Convention v e r o Configuration
    • Default Goal
    • 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
    • Super Pom
    • 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.
    • Saving time with Archetypes
    • 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=quot;org.apache.maven.archetypesquot; -DarchetypeArtifactId=maven-archetype-archetype -DarchetypeVersion=1.0 -DgroupId=com.ambientideas -DartifactId=mysamplearchetype
    • Dependency Management
    • Dirty Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.1 B:B:2.0 JU:JU:3.8.2 JU:JU:3.8.2
    • How does Maven resolve versions?
    • Nearest
    • Dirty Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.1 B:B:2.0 JU:JU:3.8.2 JU:JU:3.8.2
    • Dirty Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.1 B:B:2.0 JU:JU:3.8.2 JU:JU:3.8.2
    • Then highest version?
    • Then highest version?
    • Then sequential first at same distance
    • Dirty Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.1 B:B:2.0 JU:JU:3.8.2 JU:JU:3.8.2
    • Dirty Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.1 B:B:2.0 JU:JU:3.8.2 JU:JU:3.8.2
    • First sequential at same level ➲ [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
    • First sequential at same level ➲ 4.0 is resolved [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
    • First sequential at same level ➲ 4.0 is resolved [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
    • Resolving Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2 B:B:2.0 JU:JU:3.8.1 JU:JU:3.8.2
    • Resolving Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2 B:B:2.0 JU:JU:3.8.1
    • Resolving Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2 B:B:2.0
    • Resolved Tree A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2
    • We’ve resolved the version tree.
    • How about the dependency graph?
    • Dirty Graph A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2 B:B:2.0 JU:JU:3.8.1 JU:JU:3.8.2
    • Resolved Graph A:A:1.0 B:B:1.0 C:C:1.0 JU:JU:3.8.2
    • A dice roll for 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 declarations with <*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
    • Groovy Support
    • 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 line Help !
    • 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:eclipse File > Import > Existing ➊ Projects into Workspace Set up Eclipse Classpath Variable M2_REPO
    • mvn eclipse:eclipse File > Import > Existing ➊ Projects into Workspace Set up Eclipse Classpath Variable M2_REPO
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO mvn eclipse:m2eclipse ➋ File > Import > Existing Projects into Workspace
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO mvn eclipse:m2eclipse ➋ File > Import > Existing Projects into Workspace
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO sa ry es mvn eclipse:m2eclipse ➋ File > Importc> Existing nn e Workspace U Projects into
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO sa ry es mvn eclipse:m2eclipse ➋ File > Importc> Existing nn e Workspace U Projects into ➌ File > Import > Maven Projects
    • mvn eclipse:eclipse te d File > Import > Existing a ➊ tdClasspath Projects into Workspace u O Set up Eclipse Variable M2_REPO sa ry es mvn eclipse:m2eclipse ➋ File > Importc> Existing nn e Workspace U Projects into ➌ File > Import > Maven Projects
    • 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 Control Integration
    • 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.
    • Reactor Plugin
    • 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
    • Running Apps
    • 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=quot;com.foo.Mainquot;
    • $ mvn exec:java -Dexec.mainClass=quot;com.foo.Mainquot; or
    • $ mvn exec:java -Dexec.mainClass=quot;com.foo.Mainquot; or $ mvn exec:exec -Dexec.executable=quot;winzip.exequot; -Dexec.args=quot;myfile.txtquot;
    • Ant Integration
    • Ant Build Generation Why? ‣ Dead simple. ‣ Great for backport/legacy situations.
    • $ cd mymavenproj
    • $ cd mymavenproj $ mvn ant:ant
    • $ cd mymavenproj it! , th at’s $ mvn ant:ant Yes $ ls build.xml maven-build.xml maven-build.properties
    • 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=quot;${myspecialfolder}quot;> <fileset dir=quot;${myfiles}quot;/> </copy> </tasks> </configuration> </execution> </executions> </plugin> </plugins>
    • Sitesand Reports
    • 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
    • Twitter @matthewmccull Matthew Blog http://www.ambientideas.com/blog sidebar has all my social media links Email matthewm@ambientideas.com GitHub http://github.com/matthewmccullough
    • Resources Maven Homepage http://maven.apache.org/ Maven Sonatype eBook http://www.sonatype.com/book/ Matthew’s Maven Bookmarks http://delicious.com/matthew.mccullough/maven GMaven Plugin http://groovy.codehaus.org/GMaven
    • Resources Maven Superpom http://maven.apache.org/pom.html#The_Super_POM Sonatype Central Repo http://repository.sonatype.org/ Java.net Repo http://download.java.net/maven/2/ Archetype Mini-Guide http://maven.apache.org/guides/mini/guide-creating-archetypes.html
    • Resources GMaven Plugin http://groovy.codehaus.org/GMaven Grails Maven Integration http://www.grails.org/Maven+Integration m2eclipse Plugin Homepage http://m2eclipse.codehaus.org/ m2eclipse Eclipse Update Site http://m2eclipse.sonatype.org/update-dev/ m2eclipse Documentation http://docs.codehaus.org/display/MAVENUSER/Dealing+with+Eclipse-based+IDE
    • Resources Archetype Guide http://maven.apache.org/guides/mini/guide-creating-archetypes.html Sonatype eBook Archetype Chapter http://propellors.net/maven/book/archetypes.html http://www.sonatype.com/books/maven-book/reference/archetypes.html Archetype create-from-project Mojo http://maven.apache.org/plugins/maven-archetype-plugin/create-from-project- mojo.html JavaWorld Reporting Article http://www.javaworld.com/javaworld/jw-02-2006/jw-0227-maven.html
    • Resources Maven Dependency Plugin http://maven.apache.org/plugins/maven-dependency-plugin/ Maven AntRun Plugin http://maven.apache.org/plugins/maven-antrun-plugin/ Maven Exec Plugin http://mojo.codehaus.org/exec-maven-plugin/usage.html SuperPOM in SVN http://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
    • Image Credits ‣ http://flickr.com/photos/chanchan222/2883910690/ ‣ http://flickr.com/photos/themarmot/3173506262/ ‣ http://commons.wikimedia.org/wiki/File:Napoleon4.jpg