2. Preface Do you ever begin a new project from scratch? What’s you do before your first line code? Do you maintain the build script in your Team? Which kinds of tool do you use (make, ant, ivy, maven, mercury,…)
5. Plugin : (task in Ant) Maven is a plugin execution framework. Adding functionality to Maven is done through the plugin mechanism. (all work is done by plugins) Similar to Ant Task/Ant Target(not exactly) from the user view A collection of goals (tasks)
7. Plugin Ant Maven <targetname="compile"depends="init"description="compile the source "> <!-- Compile the java code from ${src} into ${build} --> <javacsrcdir="${src}"destdir="${build}"source="1.6"target="1.6"/> </target> <build> … <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> … </build>
8. Lifecycle : the plugin-goal (task) execution sequence <projectname="MyProject"default="dist"basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <propertyname="src"location="src"/> <propertyname="build"location="build"/> <propertyname="dist"location="dist"/> <targetname="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdirdir="${build}"/> </target> <targetname="compile"depends="init"description="compile the source "> <!-- Compile the java code from ${src} into ${build} --> <javacsrcdir="${src}"destdir="${build}"/> </target> <targetname="dist"depends="compile"description="generate the distribution"> <!-- Create the distribution directory --> <mkdirdir="${dist}/lib"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jarjarfile="${dist}/lib/MyProject-${DSTAMP}.jar"basedir="${build}"/> </target> <targetname="clean"description="clean up"> <!-- Delete the ${build} and ${dist} directory trees --> <deletedir="${build}"/> <deletedir="${dist}"/> </target> </project>
10. Lifecycle A lifecycle is made up of phases These build phases are executed sequentially (including all of the prior steps) A Build phase is made up of goals A goal represents a specific task, minimum execute unit (task) A goal is bound to zero (direct invocation) or more build phases mvn jar:jar ----------- plugin-goal prefix-name (mvngroupID:artifactID:version:goal) mvn package ----------- lifecycle phase name ,would execute jar:jar, because jar:jar is one contained goal in package phase
13. Lifecycle : adding more goals by plugin Plugin: are artifacts that provide goals (tasks) to Maven <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.7</version> <configuration> <outputDirectory>doc</outputDirectory> <show>public</show> <nohelp>true</nohelp> <windowtitle>TNT-CAL API doc</windowtitle> <doctitle>TNT-CAL API doc</doctitle> <maxmemory>256m</maxmemory> <sourcepath>src/main/java</sourcepath> <charset>utf8</charset> <locale>en_us</locale> </configuration> <executions> <execution> <id>api-doc</id> <phase>prepare-package</phase> <goals> <goal>javadoc</goal> </goals> </execution> </executions> </plugin>
14. Dependency: no flying jars No need download dependency lib one by one, declare it, Maven download for you Shrink the project size in svn <dependencies> … <dependency> <groupId>com.telenav</groupId> <artifactId>ace-client</artifactId> <version>1.0.0.15</version> <classifier>release</classifier> <type>jar</type> </dependency> <dependency> <groupId>com.telenav</groupId> <artifactId>kernel</artifactId> <version>a1.3-b101</version> <type>jar</type> </dependency> … </dependencies>
15. Dependency: transitive Compile ok, runtime ‘NoClassDefFoundError’ ---- Need transitive jar Transitive (Chain) A -> B -> C Transitive config Shortest path A -> B -> C -> D 2.0 and A -> E -> D 1.0 Dependency scope Dependency management : inheritance; import
17. Profile Different build configuration for different target environments Different OS Different deploy environment Activation -P CLI option Based on environment variables… Maven will execute the steps in the profile in addition to the normal steps
21. Demo : Hello World Build Package Test Report Install/Deploy/Release mvn install: to local repository mvndeploy: to remote (release) repository mvn release: deploy and tag the source code by scm.
28. Repository Dependent on not published jar ? mvninstall:install-file System scope (systemPath) File Repository
29. Inheritance & Multiply-module Inheritance Dependencies Plugin lists/executions/configuration Properties (Yes) Profile is not inherited directly but activated Multiply-Module (Aggregation, a single command:) <parent> <groupId>com.telenav</groupId> <artifactId>geoalert-masterpom</artifactId> <version>1.0.0</version> </parent> <modules> <module>my-project</module> <module>another-project</module> </modules>
30. Maven-Plugin Extends AbstractMojo Project Definition <packaging>maven-plugin</packaging> Parameters : inject by Maven (IOC) Annotation /* extra files/folders to classpath * @parameter */ protected File[]extraClassPathItems; <configuration> <extraClassPathItems> <extraClassPathItem>src/main/webapp</extraClassPathItem> </extraClassPathItems> </configuration> /** * Location of class files * * @parameter expression="${project.build.outputDrectory}" * @required */ private File outputDirectory;
31. ANT vs Maven Declarative (Maven) and Imperative (Ant) Convention and configuration (Maven) over configuration and scripting (Ant) Ant dist(Defaut) generate-java init clean compile <target name=“dist” depends=“generate-java, compile”/> generate-resources Maven compile compiler:compile test surefire:test Standard project layout pakcage surefire:test install Build-in Phases Build-in Goals
32. Ant vs Maven Maven Good for Modularization Dependency management Not easy for beginner to understand Bugs and issues are hard to track (understand the conventions) Sometimes are slow Ant Easy to learn – No so many abstraction
33. Reference Reference http://www.sonatype.com/books/mvnref-book/reference/public-book.html http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html Deploy use Maven http://jlorenzen.blogspot.com/2007/09/how-to-effectively-use-snapshot.html http://java.dzone.com/articles/getting-started-nexus-maven Release use Maven http://jlorenzen.blogspot.com/2007/09/how-to-create-release-using-maven2.html http://maven.apache.org/guides/mini/guide-releasing.html