Using Maven2 - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

Using Maven2

Description:

What Else is Maven? Succinct command line tool. Designed for Java ... Integrated with IDEs. Integrated with Ant. System of repositories. Project kick starter ... – PowerPoint PPT presentation

Number of Views:777
Avg rating:3.0/5.0
Slides: 54
Provided by: russel85
Category:
Tags: ides | maven2 | using

less

Transcript and Presenter's Notes

Title: Using Maven2


1
Using Maven2
2
Free Maven Books
  • Maven The Definitive Guide (alpha)?
  • www.sonatype.com/book

Better Builds with Maven www.mergere.com
3
The Maven Site
  • http//maven.apache.org/

4
What is Maven?
  • Build lifecycle
  • Dependency management tool
  • Artifact repository
  • Collection of plugins
  • Project reporting tool
  • Set of conventions
  • Distilled experience

5
What Else is Maven?
  • Succinct command line tool
  • Designed for Java/Java EE/other
  • Holder/publisher of project documentation
  • Generator of project metrics
  • Customisable environment, lifecycle, etc
  • Inheritable
  • Declarative
  • Encourager of modularity and reuse
  • Integrated with SCM tools
  • Integrated with IDEs
  • Integrated with Ant
  • System of repositories
  • Project kick starter
  • Release manager
  • Deployer
  • Enabler of portable build knowledge
  • Encourager of best practice
  • Community
  • Not perfect

6
Quick Start
  • Download Maven2, unzip, add bin directory to
    PATH
  • Configure proxy in /.m2/settings.xml if required

mvn archetypecreate \-DgroupIdcom.example
\-DartifactIdmy-app
7
Directory StructureConvention
  • Java sourcessrc/main/java
  • Unit testssrc/test/java
  • pom.xml

8
pom.xml The Project Object Model
  • ltproject xmlns"http//maven.apache.org/POM/4.0.0"
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-insta
    nce"
  • xsischemaLocation"http//maven.apache.org/POM/4
    .0.0
  • http//maven.apache.org/maven
    -v4_0_0.xsd"gt
  • ltmodelVersiongt4.0.0lt/modelVersiongt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtmy-applt/artifactIdgt
  • ltpackaginggtjarlt/packaginggt
  • ltversiongt1.0-SNAPSHOTlt/versiongt
  • ltnamegtmy-applt/namegt
  • lturlgthttp//maven.apache.orglt/urlgt
  • ltdependenciesgt
  • ltdependencygt
  • ltgroupIdgtjunitlt/groupIdgt
  • ltartifactIdgtjunitlt/artifactIdgt
  • ltversiongt3.8.1lt/versiongt
  • ltscopegttestlt/scopegt
  • lt/dependencygt
  • lt/dependenciesgt

9
Directory Structure Convention
  • Added
  • My app sources
  • Properties filesrc/main/resources/
    messages.poperties
  • My unit test

10
Killer App
  • package com.example

public class MessagePrinter public void
printMessage(String message, OutputStream os)
PrintWriter pw new PrintWriter(os)
pw.print(message) pw.flush()
public class Hello public static void
main(String args) ResourceBundle messages
ResourceBundle.getBundle("messages")
MessagePrinter mp new MessagePrinter()
mp.printMessage(messages.getString("message1"),
System.out) mp.printMessage("\n",
System.out)
11
My Application POM
  • ltproject ...gt
  • ...
  • ltnamegtMy Applicationlt/namegt
  • lturlgthttp//localhost8080/my-applt/urlgt
  • ...
  • ltbuildgt
  • ltpluginsgt
  • ltplugingt
  • ltartifactIdgtmaven-jar-pluginlt/artifactIdgt
  • ltconfigurationgt
  • ltarchivegt
  • ltmanifestgt
  • ltmainClassgtcom.example.Hellolt/mainClassgt
  • lt/manifestgt
  • lt/archivegt
  • lt/configurationgt
  • lt/plugingt
  • lt/pluginsgt
  • lt/buildgt

12
Eclipse Integration
  • Maven2 plugin for Eclipse either from project
    root execute mvn eclipseeclipseand import
    with Eclipse, or create the project in Eclipse
    and add the Maven2 project nature

13
Eclipse Integration
14
Ready for Take Off
  • mvn package

15
stdout
  • INFO Scanning for projects...
  • INFO -------------------------------------------
    ---------------------------------
  • INFO Building My Application
  • INFO task-segment package
  • INFO -------------------------------------------
    ---------------------------------
  • INFO resourcesresources
  • INFO Using default encoding to copy filtered
    resources.
  • INFO compilercompile
  • INFO Compiling 1 source file to
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/classes
  • INFO resourcestestResources
  • INFO Using default encoding to copy filtered
    resources.
  • INFO compilertestCompile
  • INFO Compiling 1 source file to
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/test-classes
  • INFO surefiretest
  • INFO Surefire report directory
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/surefire-reports
  • --------------------------------------------------
    -----
  • T E S T S
  • --------------------------------------------------
    -----

16
The (Almost)Finished Product
  • Classes and test classes compiled
  • Resources copied to classes directory
  • Test reports created
  • Jar file created

java -jar my-app-1.0-SNAPSHOT.jar Hello World!
17
Plugins Goals
  • A plugin contains one or more goals(Goal a.k.a.
    Mojo Maven Pojo Mojo ??!_at_!)?
  • A plugin is a Maven artifact
  • A goal is uniquely referenced/invoked
    bygroupIdartifactIdversiongoale.g
    org.apache.maven.pluginsmaven-eclipse-pluginecli
    psedefaults shorten this to eclipseeclipse

18
Anatomy of a Maven Command
  • 1. Invoke a specific goal
  • mvn options plugingoal parameter...
  • e.g
  • mvn -e eclipseeclipse
  • -gt Generate Eclipse configuration, print verbose
    error messages

2. Invoke goals bound to the lifecycle(s) up to
and including a phase mvn options phase...
parameter... e.g mvn clean package
-Dmaven.test.skiptrue -gt Clean target, build
package, skip tests
19
Maven Lifecycles
  • Three built-in lifecycles
  • default
  • clean
  • site
  • You can create your own lifecycle, but only if
    you have really weird build requirements!

20
The Default Build Lifecycle
21
Project Packaging
  • ltproject ...gt
  • ltmodelVersiongt4.0.0lt/modelVersiongt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtmy-applt/artifactIdgt
  • ltpackaginggtjarlt/packaginggt
  • ltversiongt1.0-SNAPSHOTlt/versiongt
  • ...
  • lt/projectgt

22
Lifecycle Bindings
23
Build Lifecycle
  • mvn package

24
stdout
  • INFO Scanning for projects...
  • INFO -------------------------------------------
    ---------------------------------
  • INFO Building My Application
  • INFO task-segment package
  • INFO -------------------------------------------
    ---------------------------------
  • INFO resourcesresources
  • INFO Using default encoding to copy filtered
    resources.
  • INFO compilercompile
  • INFO Compiling 1 source file to
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/classes
  • INFO resourcestestResources
  • INFO Using default encoding to copy filtered
    resources.
  • INFO compilertestCompile
  • INFO Compiling 1 source file to
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/test-classes
  • INFO surefiretest
  • INFO Surefire report directory
    /home/russell/Desktop/maven-presentation/example/m
    y-app/target/surefire-reports
  • --------------------------------------------------
    -----
  • T E S T S
  • --------------------------------------------------
    -----

25
Dependencies
  • ltproject ...gt
  • ...
  • ltdependenciesgt
  • ltdependencygt
  • ltgroupIdgtjunitlt/groupIdgt
  • ltartifactIdgtjunitlt/artifactIdgt
  • ltversiongt3.8.1lt/versiongt
  • ltscopegttestlt/scopegt
  • lt/dependencygt
  • lt/dependenciesgt
  • lt/projectgt
  • Explicitly declared, including version
  • Resolved by Maven, not required in project
    directory / source control repository
  • Scoped compile, provided, runtime, test
  • SNAPSHOT dependencies updated
  • Transitive
  • Strictly acyclic (a DAG not a tree)?

26
Killer App Reloaded
  • public class Hello
  • public static void main(String args)
  • ReloadableResourceBundleMessageSource
    messages
  • new ReloadableResourceBundleMessageSource
    ()
  • messages.setCacheSeconds(1)
  • messages.setBasename("messages")
  • MessagePrinter mp new MessagePrinter()
  • Scanner scanner new Scanner(System.in)
  • do
  • String message messages.getMessage("message1"
    , null,

  • Locale.getDefault())
  • mp.printMessage(message, System.out)
  • mp.printMessage("\n", System.out)
  • mp.printMessage("Keep playing? Y/n\n",
    System.out)
  • while (!"n".equals(scanner.nextLine()))

27
Dependencies
  • ltproject ...gt
  • ...
  • ltdependenciesgt
  • ltdependencygt
  • ltgroupIdgtorg.springframeworklt/groupIdgt
  • ltartifactIdgtspring-contextlt/artifactIdgt
  • ltversiongt2.0,)lt/versiongt
  • ltexclusionsgt
  • ltexclusiongt
  • ltgroupIdgtorg.springframeworklt/groupIdgt
  • ltartifactIdgtspring-aoplt/artifactIdgt
  • lt/exclusiongt
  • ...
  • lt/projectgt
  • Version ranges
  • Use exclusions to trim unwanted dependencies

28
Transitive Dependencies
29
Reloadable Message Source
  • Hello World!
  • Keep playing? Y/n
  • y
  • Hello Again World!
  • Keep playing? Y/n
  • n
  • Note for anyone trying this at home there's a
    bit of classpath config required to get this
    working nicely. It's easiest to run it on the
    Eclipse console, and modify the target
    messages.properties

30
Configuring Pluginse.g. Ant
  • ltproject...gt
  • ...
  • ltbuildgt
  • ltpluginsgt
  • ltplugingt
  • ltartifactIdgtmaven-antrun-pluginlt/artifactIdgt
  • ltexecutionsgt
  • ltexecutiongt
  • ltphasegtgenerate-sourceslt/phasegt
  • ltconfigurationgt
  • lttasksgt
  • lt!--
  • Place any ant task here. You can add
    anything you can add
  • between lttargetgt and lt/targetgt in a
    build.xml.
  • --gt
  • lt/tasksgt
  • lt/configurationgt
  • ltgoalsgt
  • ltgoalgtrunlt/goalgt

31
StandardMaven Plugins
Listed at http//maven.apache.org/plugins/
  • clean
  • compiler
  • deploy
  • install
  • resources
  • site
  • surefire
  • verifier
  • ear
  • ejb
  • jar
  • rar
  • war
  • changelog

changes checkstyle clover doap docck javadoc jxr p
md project-info-reports surefire-report ant antlr
antrun archetype
assembly dependency enforcer gpg help invoker one
plugin release remote-resources repository scm so
urce eclipse
idea Codehaus build-helper castor javacc jdepend
native sql taglist Other cargo jaxme jetty jalopy
32
POM Inheritance
  • ltprojectgt
  • ltmodelVersiongt4.0.0lt/modelVersiongt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtmy-applt/artifactIdgt
  • ltversiongt1.0-SNAPSHOTlt/versiongt
  • lt/projectgt

No source or target defined in POM, yet this
works mvn compile
33
Super POM
  • ltprojectgt
  • ltbuildgt
  • ...
  • ltoutputDirectorygttarget/classeslt/outputDirectory
    gt
  • ...
  • ltsourceDirectorygtsrc/main/javalt/sourceDirectorygt
  • ...
  • ltresourcesgt
  • ltresourcegt
  • ltdirectorygtsrc/main/resourceslt/directorygt
  • lt/resourcegt
  • lt/resourcesgt
  • ...
  • lt/buildgt
  • lt/projectgt

To fully resolve the POM mvn
helpeffective-pom less
34
POM Inheritance
ltprojectgt ltgroupIdgtcom.examplelt/groupIdgt
ltartifactIdgtorg-pomlt/artifactIdgt
ltversiongt1lt/versiongt lt!-- configure JDK
version, e.g 1.5 standard reports, etc.
--gt lt/projectgt
  • ltprojectgt
  • ltparentgt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtorg-pomlt/artifactIdgt
  • ltversiongt1lt/versiongt
  • lt/parentgt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtmy-applt/artifactIdgt
  • ...
  • lt/projectgt

35
Maven Repositories
  • Repositories store artifacts
  • plugins
  • project dependencies
  • Central http//repo1.maven.org/maven2(or
    mirror)?
  • Local /.m2/repository
  • The first execution of a plugin, or requirement
    for a dependency pulls the artifact from central
    and caches it locally

36
Maven Repositories
  • Problems
  • Reliant on network and external repository for
    dependencies and plugins
  • Can't deploy to Central Maven repository for
    reuse as dependencies of other projects (though
    usually wouldn't want to)?

37
Organisation Repository
  • No longer reliant on network or external
    repository for dependencies and plugins
  • Can deploy to organisation repository in order to
    share artifacts
  • Multiple repository configurations possible
  • Multiple repository tools available Archiva,
    Proximity, Artifactory

38
Archiva
39
Install and Deploy
  • mvn deploy

40
Install and Deploy
  • mvn deploy

41
SCM Integration
  • Fully implemented
  • Bazaar
  • CVS
  • Mercurial
  • Perforce
  • StarTeam
  • Subversion
  • CM Synergy
  • Partially implemented
  • ClearCase
  • File system
  • Visual Source Safe

42
Configuring SCM
  • ltprojectgt
  • ltgroupIdgtcom.examplelt/groupIdgt
  • ltartifactIdgtmy-applt/artifactIdgt
  • ...
  • ltscmgt
  • ltconnectiongt
  • scmsvnhttp//example.com/svn-read/my-app/trun
    k
  • lt/connectiongt
  • ltdeveloperConnectiongt
  • scmsvnhttp//example.com/svn-dev/my-app/trunk
  • lt/developerConnectiongt
  • lturlgt
  • http//example.com/view.cvs
  • lt/urlgt
  • lt/scmgt
  • lt/projectgt

43
SCM Integration,What For?
  • Use SCM agnostic commands mvn scmcheckin
    -Dmessage"to the cause" mvn scmupdate
  • Project bootstrapping mvn scmbootstrap
  • Available for use by Maven tools, e.g documented
    and linked in project website, published in
    Archiva summary
  • Continuous Integration, SCM details located in
    project rather than CI tool
  • Release management

44
Cutting a Release
  • mvn releaseprepare -DdryRuntrue
  • Checks SCM for modifications
  • Checks for use of SNAPSHOT dependencies or
    plugins
  • Runs mvn clean integration-test
  • Requests release info
  • version numbers
  • Creates new POMs
  • pom.xml for tag
  • pom.xml for next version
  • release-pom.xml
  • Creates tag in SCM
  • mvn releaseperform
  • Uses release-pom.xml, deploysproject, generates
    site, etc.

45
Website / Reports (1)?
  • Project website
  • Conventions for structuring documentation,
    supporting various formats DocBook simple, FML,
    XDOC, APT, Twiki
  • Directory structure conventions, supporting
    multiple types of documentation, e.g public,
    user, developer, etc.
  • Configurable, skinnable site
  • Project info from POM contact details
    organisation, developers SCM details, etc.

46
Website / Reports (2)?
  • Metrics, checks, and project reports (on
    website)
  • Javadoc
  • Test results
  • Code coverage (Cobertura)?
  • Checkstyle, PMD, JDepend, Clirr
  • Database documentation (Hibernate)?
  • Dependency report
  • TODO report (//TODO, FIXME, configurable)?
  • Linked and line-numbered HTML sources
  • Release notes and roadmap from issue tracker

47
Quick Tour
48
In Brief (1)?
  • Java EE support
  • WAR, EAR packaging
  • Rapid web app development
  • Integration (in container) testing
  • Deployment to environments
  • Multi-module projects
  • Enable / encourage reuse between projects
  • Maven inter-module dependency eliminates cycles
    between modules
  • Nicely supported in NetBeans
  • Not nicely supported in Eclipse nested projects

49
In Brief (2)?
  • Continuous Integration
  • CruiseControl
  • Continuum
  • Reuses project information as defined in POM
  • Profiles
  • Build activity carried out under different
    conditions, e.g personal requirements, dev /
    test / release, continuous integration
  • Maven settings
  • Help

50
Problems
  • History Maven 1, might have left a bad taste
  • Steep learning curve
  • Once you've got it, the knowledge is portable to
    other projects built with Maven
  • Complex needs require complex configuration
  • Alan Kay Simple things should be simple. Complex
    things should be possible
  • Verbose XML config
  • Docs aren't great, but getting better
  • Error messages often do not provide much (or any)
    detail (e.g. archetype problem)?
  • Ensuring the project is buildable and testable in
    the IDE as well as with Maven can be complex
  • Multi-module projects not supported by Eclipse
    (but they are in Netbeans)?

51
Stuff to Look at
  • Buildr a build system configured in Ruby that
    reuses parts of Maven
  • repositories
  • directory structure conventions
  • Rake a Ruby build tool

52
Still Interested?
  • Get reading and enjoy the ride

53
Questions?
Write a Comment
User Comments (0)
About PowerShow.com