Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Maven Inside Out

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Maven Inside Out

Avatar for Jestine Paul

Jestine Paul

August 21, 2013

Other Decks in Programming

Transcript

  1. Coordinates & Dependencies Plugins & Build Lifecycle Maven Inside Out

    Jestine Paul 23rdAugust, 2013 Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  2. Coordinates & Dependencies Plugins & Build Lifecycle Outline 1 Coordinates

    & Dependencies Maven Coordinates Super POM Dependency Mechanism Multi-Module Project 2 Plugins & Build Lifecycle Plugin Internals Build Lifecycle Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  3. Coordinates & Dependencies Plugins & Build Lifecycle Introduction Convention over

    configuration. Eg. Ruby on Rails, EJB 3.0 Special variable project.basedir - The directory that the current project resides in. ${project.basedir }/ src/main/java /resources /test/java /resources /scripts /target/classes /test -classes /site Defaults without configuring. Apache Ant has to explicitly define these locations Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  4. Coordinates & Dependencies Plugins & Build Lifecycle Core Of Maven

    Is Dumb Just parses a few XML files and keeps track of life cycle and plugins. Action happens in plugin goals - compilation, testing, producing JAR files, reporting, etc. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  5. Coordinates & Dependencies Plugins & Build Lifecycle Setting Up Maven

    M2 HOME=/home/jestinepaul/apache-maven-3.1.0 PATH=${PATH}:${M2 HOME}/bin MAVEN OPTS= -Xms256m -Xmx512m Verifing the installation, mvn -version Global settings - ${MAVEN HOME}/conf/settings.xml User settings - ∼/.m2/settings.xml Effective setting of a user, mvn help:effective -settings Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  6. Coordinates & Dependencies Plugins & Build Lifecycle settings.xml I <?xml

    version="1.0" encoding="UTF -8"?> <settings xmlns="http: // maven.apache.org/SETTINGS /1.0.0" ........ > <localRepository >/path/to/local/repo </ localRepository > <interactiveMode >true </ interactiveMode > <offline >false </offline > <pluginGroups > ... </ pluginGroups > <proxies > ... </proxies > <servers > ... </servers > <mirrors > ... </mirrors > <profiles > ... </profiles > <activeProfiles > ... </ activeProfiles > </settings > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  7. Coordinates & Dependencies Plugins & Build Lifecycle Project Object Model

    - POM I <project xmlns =" http :// maven.apache.org/POM /4.0.0" ........ > <modelVersion >4.0.0 </ modelVersion > <!-- The Basics --> <groupId >... </ groupId > <artifactId >... </ artifactId > <version >... </ version > <packaging >... </ packaging > <dependencies >... </ dependencies > <parent >... </ parent > <dependencyManagement >... </ dependencyManagement > <modules >... </ modules > <properties >... </ properties > <!-- Build Settings --> <build >... </ build > <reporting >... </ reporting > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  8. Coordinates & Dependencies Plugins & Build Lifecycle Project Object Model

    - POM II <!-- More Project Information --> <name >... </name > <description >... </ description > <url >... </url > <inceptionYear >... </ inceptionYear > <licenses >... </ licenses > <organization >... </ organization > <developers >... </ developers > <contributors >... </ contributors > <!-- Environment Settings --> <issueManagement >... </ issueManagement > <ciManagement >... </ ciManagement > <mailingLists >... </ mailingLists > <scm >... </scm > <prerequisites >... </ prerequisites > <repositories >... </ repositories > <pluginRepositories >... </ pluginRepositories > <distributionManagement >... </ distributionManagement > <profiles >... </ profiles > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  9. Coordinates & Dependencies Plugins & Build Lifecycle Project Object Model

    - POM III </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  10. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Maven Coordinates groupId unique amongst an organization or a project generally resemble a Java package name artifactId name that the project is known by Avoid using dot in artifactId version major.minor.incremental-qualifier If the version contains the string SNAPSHOT, it gets expanded to UTC date and time value packaing - jar, pom, maven-plugin, ejb, war, ear, rar, par classifier - eg. Maven identifies binary and source Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  11. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Super POM Maven always execute against an effective POM - combination of project , parent and super POM. mvn help:effective -pom All Maven pom inherits the super pom. pom-4.0.0.xml in the lib/maven-model-builder-3.0.3¬ .jar:org/apache/maven/model/pom-4.0.0.xml Defines Maven central repository - contains official releases of Maven core plugins java.net , oracle, .. Default values for directories Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  12. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project pom-4.0.0.xml I <project > <modelVersion >4.0.0 </ modelVersion > <repositories > <repository > ... </repository > </ repositories > <pluginRepositories > <pluginRepository > ... </ pluginRepository > </ pluginRepositories > <build > <directory >${ project.basedir }/ target </directory > <outputDirectory >${ project.build.directory }/ classes </ outputDi <finalName >${ project.artifactId }-${ project.version}</finalNam Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  13. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project pom-4.0.0.xml II <testOutputDirectory >${ project.build.directory }/test -classes < <sourceDirectory >${ project.basedir }/ src/main/java </ sourceDire <scriptSourceDirectory >${ project.basedir }/ src/main/scripts </ s <testSourceDirectory >${ project.basedir }/ src/test/java </ testSo <resources > <resource > <directory >${ project.basedir }/ src/main/resources </directo </resource > </resources > <testResources > <testResource > <directory >${ project.basedir }/ src/test/resources </directo </ testResource > </ testResources > <pluginManagement > <plugins > <plugin > .... <plugin > <artifactId >maven -dependency -plugin </ artifactId > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  14. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project pom-4.0.0.xml III <version >2.8 </version > </plugin > </plugins > </ pluginManagement > </build > <reporting > <outputDirectory >${ project.build.directory }/ site </ outputDirec </reporting > <profiles > ... <profile > <id>release -profile </id> <activation > <property > <name >performRelease </name > <value >true </value > </property > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  15. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project pom-4.0.0.xml IV </activation > <build > <plugins > <plugin > <inherited >true </inherited > <artifactId >maven -source -plugin </ artifactId > <executions > <execution > <id>attach -sources </id> <goals > <goal >jar </goal > </goals > </execution > </executions > </plugin > ... </plugins > </build > </profile > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  16. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project pom-4.0.0.xml V </profiles > </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  17. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Dependency Resolution mvn dependency :tree -Dverbose -Dincludes=commons - collections The verbose flag instructs the dependency tree to display conflicting dependencies that were omitted from the resolved dependency tree [INFO] [dependency:tree] [INFO] org.apache.maven.plugins:maven -dependency -plugin:maven -plug [INFO] +- org.apache.maven.reporting:maven -reporting -impl:jar :2.0. [INFO] | \- commons -validator:commons -validator:jar :1.2.0: compile [INFO] | \- commons -digester:commons -digester:jar :1.6: compile [INFO] | \- (commons - collections :commons - collections :jar :2. [INFO] \- org.apache.maven.doxia:doxia -site -renderer:jar :1.0 - alpha [INFO] \- org.codehaus.plexus:plexus -velocity:jar :1.1.3: compile [INFO] \- commons - collections :commons - collections :jar :2.0: co Thus we can see that Commons Collections 2.0 was chosen over 2.1 since it is nearer, and by default Maven resolves version conflicts with a nearest-wins strategy. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  18. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Property <properties > <dbDriver >postgresql </ dbDriver > </properties > or mvn ... -DdDriver=mysql Used as ${dbDriver} to define POM values except the coordinates. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  19. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Implicit Properties project.* - POM values settings.* - settings.xml values env.* - environment value java.version, os.arch, etc. - System.getProperty() Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  20. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Multi-Module Project Aggregation <modules > <module >simple -weather </ module > <module >simple -webapp </ module > </modules > Inheritance <parent > <groupId >org.sonatype.mavenbook.ch06 </ groupId > <artifactId >simple -parent </ artifactId > <version >1.0 </ version > </parent > Inheritance and aggregation are two separate features that can be used separately, however they are frequently used together. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  21. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-parent project POM I <project xmlns =" http :// maven.apache.org/POM /4.0.0" xmlns:xsi=" http :// www.w3.org /2001/ XMLSchema -instance" xsi: schemaLocation =" http :// maven.apache.org/POM /4.0.0 http :// maven.apache.org/maven -v4_0_0.xsd"> <modelVersion >4.0.0 </ modelVersion > <groupId >org.mavendemo </ groupId > <artifactId >simple -parent </ artifactId > <packaging >pom </ packaging > <version >1.0 </ version > <name >Simple Parent Project </name > <modules > <module >simple -first -child </ module > <module >simple -second -child </ module > </modules > <dependencyManagement > <dependencies > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  22. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-parent project POM II <dependency > <groupId >org.springframework </ groupId > <artifactId >spring -core </ artifactId > <version >3.10 </ version > <exclusions > <exclusion > <artifactId >commons -logging </ artifactId > <grouptId >commons -logging </ grouptId > </exclusion > </exclusions > </dependency > </dependencies > </ dependencyManagement > </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  23. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-first-child project POM I <project xmlns =" http :// maven.apache.org/POM /4.0.0" xmlns:xsi=" http :// www.w3.org /2001/ XMLSchema -instance" xsi: schemaLocation =" http :// maven.apache.org/POM /4.0.0 http :// maven.apache.org/maven -v4_0_0.xsd"> <modelVersion >4.0.0 </ modelVersion > <parent > <groupId >org.mavendemo </ groupId > <artifactId >simple -parent </ artifactId > <version >1.0 </ version > </parent > <artifactId >simple -first -child </ artifactId > <packaging >jar </ packaging > <name >Simple First Child API </name > <dependencies > <dependency > <groupId >org.springframework </ groupId > <artifactId >spring -core </ artifactId > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  24. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-first-child project POM II </dependency > <dependency > <groupId >velocity </ groupId > <artifactId >velocity </ artifactId > <version >1.5 </ version > </dependency > </dependencies > </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  25. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-second-child project POM I <project xmlns =" http :// maven.apache.org/POM /4.0.0" xmlns:xsi=" http :// www.w3.org /2001/ XMLSchema -instance" xsi: schemaLocation =" http :// maven.apache.org/POM /4.0.0 http :// maven.apache.org/maven -v4_0_0.xsd"> <modelVersion >4.0.0 </ modelVersion > <parent > <groupId >org.mavendemo </ groupId > <artifactId >simple -parent </ artifactId > <version >1.0 </ version > </parent > <artifactId >simple -second -child </ artifactId > <packaging >jar </ packaging > <name >Simple Second Child </name > <dependencies > <dependency > <groupId >${project.groupId }</groupId > <artifactId >simple -first -child </ artifactId > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  26. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project simple-second-child project POM II <version >${project.version }</version > </dependency > </dependencies > </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  27. Coordinates & Dependencies Plugins & Build Lifecycle Maven Coordinates Super

    POM Dependency Mechanism Multi-Module Project Multi-Module Project Inheritance and aggregation are two separate features that can be used separately, however they are frequently used together. Maven reactor takes care of the order in the <modules> tag Child project can omit <version> and <exclusion> if <dependencyManagament> tag is defined in parent pom, Use <pluginManagement> tag in parent pom to eliminate duplicate plugin configuration in child pom Use built-in ${project.version} ${project.groupId} when referring to a sibling project. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  28. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Plugins Naming convention xxxxx-maven-plugin for official Apache Maven plugins, maven-xxxxx-plugin Two types: Build - configured in <build> Reporting - configured in <reporting> Help goal mvn javadoc:help -Ddetail -Dgoal=javadoc shows the help for javadoc:javadoc goal Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  29. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle GreetingMojo I package sample.plugin; import org.apache.maven.plugin. AbstractMojo ; import org.apache.maven.plugin. MojoExecutionException ; import org.apache.maven.plugins. annotations .Mojo; /** * @goal sayhi * @phase package */ public class GreetingMojo extends AbstractMojo { // When reading the configuration , // the text "true" causes the parameter to be set to true // and all other text causes the parameter to be set to false. @Parameter private boolean myBoolean; // byte , Byte , int , Integer , long , Long , short , and Short. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  30. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle GreetingMojo II // converted to an integer value using // either Integer.parseInt () // or the valueOf () method of the appropriate class. @Parameter private Integer myInteger; // double , Double , float , and Float // converted to binary form using the // valueOf () method for the appropriate class @Parameter private Double myDouble; // parsing is done using DateFormat.parse () @Parameter private Date myDate; // If the path is relative // (does not start with / or a drive letter like C:), // the path is relative to the directory containing the POM @Parameter Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  31. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle GreetingMojo III private File myFile; // scheme :// host:port/path/to/file @Parameter private URL myURL; // plain text // char , Character , StringBuffer and String @Parameter private String [] myArray; // any class which implements java.util. Collection // such as ArrayList or HashSet @Parameter private List myList; // any class which implements java.util.Map such as HashMap // but does not implement java.util. Properties @Parameter Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  32. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle GreetingMojo IV private Map myMap; @Parameter private Properties myProperties ; @Parameter private MyObject myObject; public void execute () throws MojoExecutionException { getLog (). info( "Hello , world." ); } } Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  33. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Plugin pom.xml <project > <modelVersion >4.0.0 </ modelVersion > <groupId >sample.plugin </groupId > <artifactId >hello -maven -plugin </ artifactId > <version >1.0- SNAPSHOT </version > <packaging >maven -plugin </packaging > <name >Sample Parameter -less Maven Plugin </name > <dependencies > <dependency > <groupId >org.apache.maven </groupId > <artifactId >maven -plugin -api </ artifactId > <version >2.0 </version > </dependency > </ dependencies > </project > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  34. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Our Project pom.xml I ... <build > <plugins > <plugin > <groupId >sample.plugin </groupId > <artifactId >hello -maven -plugin </ artifactId > <version >1.0- SNAPSHOT </version > <executions > <execution > <id>execution1 </id> <phase >install </phase > <configuration > <myBoolean >true </myBoolean > <myInteger >10</myInteger > <myDouble >1.0 </myDouble > <myDate >2005 -10 -06 2:22:55 .1 PM</myDate > <myFile >c:\temp </myFile > <myURL >http: // maven.apache.org</myURL > <myArray > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  35. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Our Project pom.xml II <param >value1 </param > <param >value2 </param > </myArray > <myList > <param >value1 </param > <param >value2 </param > </myList > <myMap > <key1 >value1 </key1 > <key2 >value2 </key2 > </myMap > <myProperties > <property > <name >propertyName1 </name > <value >propertyValue1 </value > </property > <property > <name >propertyName2 </name > <value >propertyValue2 </value > </property > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  36. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Our Project pom.xml III </ myProperties > </ configuration > <goals > <goal >sayhi </goal > </goals > </execution > </executions > </plugin > </plugins > </build > ... Now, GreetingMojo default phase which is package has been overrided by install phase. Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  37. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Invoking the Mojo mvn sample.plugin:hello -maven -plugin :1.0 - SNAPSHOT:sayhi Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  38. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Shortening the Command Line mvn hello:sayhi omit version - takes the latest shortened-prefix - if prefix-maven-plugin naming convention is followed to omit groupid - ${user.home}/.m2/settings.xml file: <pluginGroups > <pluginGroup >sample.plugin </ pluginGroup > </pluginGroups > Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  39. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Build Lifecycle Organised sequence of phases that exist to give order to a set of goals packaged in maven plugin objects. Maven core defines three lifecycles clean - removed all files from ${project.build.directory} default - generates artifacts into ${project.build.directory} site - generates documentation website Each life cycle is an ordered list of phases. Execution of one phase will trigger ordered list of previous phases. dependency Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  40. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Standard Life Cycle I clean pre-clean clean post-clean default validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  41. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Standard Life Cycle II generate-test-resources process-test-resources process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy site pre-site site Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  42. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Standard Life Cycle III post-site site-deploy Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  43. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Packaging jar, war process-resources resources:resources compile compiler:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package jar:jar or war:war install install:install deploy deploy:deploy Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  44. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Packaging maven-plugin generate-resources plugin:descriptor process-resources resources:resources compile compiler:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package jar:jar and plugin:addPluginArtifactMetadata install install:install deploy deploy:deploy Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  45. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Packaging pom package site:attach-descriptor install install:install deploy deploy:deploy pom packaging does not produce any artifact aggregates dependencies used in parent pom Jestine Paul Overview of Dependency Mechanism and Plugin Framework
  46. Coordinates & Dependencies Plugins & Build Lifecycle Plugin Internals Build

    Lifecycle Dependency Scope Specify whether a dependency is involved or not in following phases of build life cycle compile test package Dependency scope values compile - compile, test, package provided - compile, test test - test runtime - package system Import Jestine Paul Overview of Dependency Mechanism and Plugin Framework