Published on

introduction to maven

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • mvncompilemvn packagemvn testmvneclipse:elipse
  • Maven

    1. 1.<br />MAVEN<br />
    2. 2. Preface<br />Do you ever begin a new project from scratch?<br />What’s you do before your first line code?<br />Do you maintain the build script in your Team?<br />Which kinds of tool do you use (make, ant, ivy, maven, mercury,…)<br />
    3. 3. Outline<br />Key concept<br />Basic Usage<br />Demo<br />20% usage<br />Ant vs Maven<br />
    4. 4. Key concept<br />
    5. 5. Plugin : (task in Ant)<br />Maven is a plugin execution framework. <br />Adding functionality to Maven is done through the plugin mechanism. (all work is done by plugins)<br />Similar to Ant Task/Ant Target(not exactly) from the user view <br />A collection of goals (tasks)<br />
    6. 6. Plugin<br />
    7. 7. Plugin<br />Ant<br />Maven<br /><targetname="compile"depends="init"description="compile the source "><br /><!-- Compile the java code from ${src} into ${build} --><br /><javacsrcdir="${src}"destdir="${build}"source="1.6"target="1.6"/><br /></target><br /><build><br />…<br /><plugins><br /><plugin><br /><artifactId>maven-compiler-plugin</artifactId><br /><version>2.1</version><br /><configuration><br /><source>1.6</source><br /><target>1.6</target><br /></configuration><br /></plugin><br /></plugins><br />…<br /></build><br />
    8. 8. Lifecycle : the plugin-goal (task) execution sequence<br />phases<br />goals( pluginname:goalname)<br />default<br /><projectname="MyProject"default="dist"basedir="."><br /><description><br /> simple example build file<br /></description><br /><!-- set global properties for this build --><br /><propertyname="src"location="src"/><br /><propertyname="build"location="build"/><br /><propertyname="dist"location="dist"/><br /><targetname="init"><br /><!-- Create the time stamp --><br /><tstamp/><br /><!-- Create the build directory structure used by compile --><br /><mkdirdir="${build}"/><br /></target><br /><targetname="compile"depends="init"description="compile the source "><br /><!-- Compile the java code from ${src} into ${build} --><br /><javacsrcdir="${src}"destdir="${build}"/><br /></target><br /><targetname="dist"depends="compile"description="generate the distribution"><br /><!-- Create the distribution directory --><br /><mkdirdir="${dist}/lib"/><br /><!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --><br /><jarjarfile="${dist}/lib/MyProject-${DSTAMP}.jar"basedir="${build}"/><br /></target><br /><targetname="clean"description="clean up"><br /><!-- Delete the ${build} and ${dist} directory trees --><br /><deletedir="${build}"/><br /><deletedir="${dist}"/><br /></target><br /></project><br />
    9. 9. Lifecycle<br />A lifecycle is made up of phases<br />These build phases are executed sequentially (including all of the prior steps)<br />A Build phase is made up of goals<br />A goal represents a specific task, minimum execute unit (task)<br />A goal is bound to zero (direct invocation) or more build phases<br />mvn jar:jar ----------- plugin-goal prefix-name (mvngroupID:artifactID:version:goal)<br />mvn package ----------- lifecycle phase name ,would execute jar:jar, because jar:jar is one contained goal in package phase<br />
    10. 10. Lifecycle: build-in lifecycle<br />clean<br />default<br />site<br />
    11. 11. Lifecycle: default binding goals<br />
    12. 12. Lifecycle : adding more goals by plugin<br />Plugin: are artifacts that provide goals (tasks) to Maven<br /> <plugin><br /><groupId>org.apache.maven.plugins</groupId><br /><artifactId>maven-javadoc-plugin</artifactId><br /><version>2.7</version><br /><configuration><br /><outputDirectory>doc</outputDirectory><br /><show>public</show><br /><nohelp>true</nohelp><br /><windowtitle>TNT-CAL API doc</windowtitle><br /><doctitle>TNT-CAL API doc</doctitle><br /><maxmemory>256m</maxmemory><br /><sourcepath>src/main/java</sourcepath><br /><charset>utf8</charset><br /><locale>en_us</locale><br /></configuration><br /><executions><br /><execution><br /><id>api-doc</id><br /><phase>prepare-package</phase><br /><goals><br /><goal>javadoc</goal><br /></goals><br /></execution><br /></executions><br /></plugin><br />
    13. 13. Dependency: no flying jars<br />No need download dependency lib one by one, declare it, Maven download for you<br />Shrink the project size in svn<br /><dependencies><br />…<br /><dependency><br /><groupId>com.telenav</groupId><br /><artifactId>ace-client</artifactId><br /><version></version><br /><classifier>release</classifier><br /><type>jar</type><br /></dependency><br /><dependency><br /><groupId>com.telenav</groupId><br /><artifactId>kernel</artifactId><br /><version>a1.3-b101</version><br /><type>jar</type><br /></dependency><br />…<br /></dependencies><br />
    14. 14. Dependency: transitive<br />Compile ok, runtime ‘NoClassDefFoundError’ ---- Need transitive jar<br />Transitive (Chain) A -> B -> C<br />Transitive config<br />Shortest path A -> B -> C -> D 2.0 and A -> E -> D 1.0<br />Dependency scope<br />Dependency management : inheritance; import<br />
    15. 15. Dependency: transitive<br />Dependency scope (IVY configuration map)<br />C (compile)<br />B (compile)<br />A<br />D (runtime)<br />E (test)<br />compile<br />test<br />runtime<br />
    16. 16. Profile<br />Different build configuration for different target environments<br />Different OS<br />Different deploy environment<br />Activation<br /> -P CLI option<br />Based on environment variables…<br />Maven will execute the steps in the profile in addition to the normal steps<br />
    17. 17. Profile<br /><profiles><br /><profile><br /><id>cn-dev</id><br /><activation><br /><property><br /><name>target</name><br /><value>cn-dev</value><br /></property><br /></activation><br /><properties><br /><encoding>gb2312</encoding><br /></properties><br /></profile><br /><profile><br /><id>us-deploy</id><br /><activation><br /><property><br /><name>target</name><br /><value>us-deploy</value><br /></property><br /></activation><br /> <properties><br /> <skipTests>true</skipTests><br /> </properties><br /></profile><br /></profiles><br />
    18. 18. Basic Usage<br />
    19. 19. Create a project<br />Create<br />mvn archetype:generate –DarchetypeArtifactId=maven-archetype-quickstart –DgroupId=com.telenav –DartificatId=rgc<br />Project layout<br /><br />
    20. 20. Demo : Hello World<br />Build<br />Package<br />Test<br />
    21. 21. 20% usage<br />Eclipse plugin<br />Properties<br />Repository<br />Inheritance & Multiply-module<br />Write a maven-plugin<br />
    22. 22. Eclipse-plugin m2eclipse<br /><br />Add-dependency (search)<br />Add-plugin<br />
    23. 23. Properties<br />Build in properties<br />${basedir} represents the directory containing pom.xml<br />${project.basedir} represents the directory containing pom.xml<br />Project properties<br /><project><version>1.0</version></project> is accessible via ${project.version}.<br />Environment variables<br />${env.PATH}<br />Java System Properties<br />${file.separator}<br />User-defined Properties<br />Plugin property<br />expression: ${}<br />Setting property<br />
    24. 24. Repository<br />Add repository<br />Distributionartifacts to release repository<br />Dependent on not published jar ?<br />mvn install:install-file<br />System scope (systemPath)<br />File Repository<br /><repositories><br /> <repository><br /> <id>telenav</id><br /> <url></url><br /> <releases><br /> <enabled>true</enabled><br /> </releases><br /> <snapshots><br /> <enabled>false</enabled><br /> </snapshots><br /> </repository> <br /></repositories><br /><distributionManagement><br /> <repository> <br /> <id>corp1</id> <br /> <name>Corporate Repository</name> <br /> <url>scp://repo1/maven2</url> <layout>default</layout> <br /> </repository> <br /></repositories><br />mvn install:install-file -Dfile=<path-to-file> <br />-DgroupId=<group-id> <br />-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true<br />
    25. 25. Inheritance & Multiply-module<br />Inheritance<br />Dependencies<br />Plugin lists/executions/configuration<br />Properties (Yes)<br />Profile is not inherited directly but activated<br />Multiply-Module (Aggregation, a single command:)<br /><parent><br /><groupId>com.telenav</groupId><br /><artifactId>geoalert-masterpom</artifactId><br /><version>1.0.0</version><br /></parent><br /><modules> <br /> <module>my-project</module> <br /> <module>another-project</module> <br /></modules><br />
    26. 26. Maven-Plugin<br />Extends AbstractMojo<br />Project Definition<br /><packaging>maven-plugin</packaging><br />Parameters : inject by Maven (IOC)<br />Annotation<br />/* extra files/folders to classpath<br /> * @parameter<br /> */<br />protected File[]extraClassPathItems;<br /><configuration><br /><extraClassPathItems><br /><extraClassPathItem>src/main/webapp</extraClassPathItem><br /></extraClassPathItems><br /></configuration><br /> /**<br /> * Location of class files<br /> *<br /> * @parameter expression="${}"<br /> * @required<br /> */<br />private File outputDirectory;<br />
    27. 27. ANT vs Maven<br />Declarative (Maven) and Imperative (Ant)<br />Convention and configuration (Maven) over configuration and scripting (Ant)<br />Ant<br />dist(Defaut)<br />generate-java<br />init<br />clean<br />compile<br /><target name=“dist” depends=“generate-java, compile”/><br />generate-resources<br />Maven<br />compile<br />compiler:compile<br />test<br />surefire:test<br />Standard project layout<br />pakcage<br />surefire:test<br />install<br />(Fix)Building Phases<br />Building Goals <br />
    28. 28. Ant vs Maven<br />Maven<br />Good for Modularization<br />Dependency management<br />Not easy for beginner to understand<br />Bugs and issues are hard to track (understand the conventions)<br />Sometimes are slow<br />Ant<br />Easy to learn – No so many abstraction<br />
    29. 29. Reference<br /><br /><br />