Successfully reported this slideshow.
Your SlideShare is downloading. ×

Maven basic concept

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Maven for Dummies
Maven for Dummies
Loading in …3
×

Check these out next

1 of 38 Ad

More Related Content

Slideshows for you (20)

Advertisement

Recently uploaded (20)

Advertisement

Maven basic concept

  1. 1. Maven
  2. 2. Outline • POM • Dependency • Life cycle • Plugin • Profile • SCM
  3. 3. Maven • Dependency Management • Remote Repositories • Universal Reuse of Build Logic • Tool Portablility/Integration • Easy Searching and Filtering of Project Artifact
  4. 4. Project Object Model (POM) <project> <modelVersion>4.0.0</modelVersion> <groupId>com.estinet</groupId> <artifactId>controller</artifactId> <version>1.0.0</version> </project> •groupId •Group identifier •Java package name •arti •project main identifier
  5. 5. Effective POM • mvn help:effective-pom • de Super POM com.estinet.controller bod 1.0-SNAPSHOT com.estinet.switch broadcom 1.0-SNAPSHOT
  6. 6. Project Inheritance • dependencies • developers and contributors • plugin lists (including reports) • plugin executions with matching ids • plugin configuration • resources
  7. 7. Project Inhertance <project> <parent> <groupId>com.estinet</groupId> <artifactId>controller</artifactId> <version>1.0.0</version> </parent> <artifactId>bod</artifactId> </project>
  8. 8. SNAPSHOT Version • Expand SNAPSHOT to UTC date and time • e.g., – 1.0-SNAPSHOT – 1.0-20160616-150000-1
  9. 9. Properties • env – exposed by OS ${evn.PATH} • project – project variable, defined in POM ${project.groupId} • setting – setting variable – ./.m2/setting.xml ${setting.offline}
  10. 10. Project Dependency <project> ... <dependencies> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-java5</artifactId> <version>1.2.5</version> </dependency> <dependency> ... </dependency> </dependencies> </project>
  11. 11. Dependency Scope • compile – default • provided – required on compilation, but not for execution – provided by JDK or other container • runtime – required for execution and test • test – for test only • system – like provided – must supply systemPath
  12. 12. Grouping Dependencies <project> <groupId>com.estinet.controller</groupId> <artifactId>controller-dependencies</artifactId> <version>1.0.0</version> <packaging>POM<packaging> <dependencies> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-java5</artifactId> <version>1.2.5</version> </dependency> <dependency> ... </dependency> </dependencies> </project> install POM to your local repository (~/.m2) mvn install
  13. 13. Grouping Dependencies <project> ... <dependencies> <dependency> <groupId>com.estinet.controller</groupId> <artifactId>controller-dependencies</artifactId> <version>1.0.0</version> <type>POM</type> </dependency> <dependency> ... </dependency> </dependencies> </project>
  14. 14. Multi-Modules <project> ... <modules> <module>simple-weather</module> <module>simple-webapp</module> </modules> </project>
  15. 15. Maven Usages usage: mvn [options] [<goal(s)>] [<phase(s)>]
  16. 16. Lifecycle lifecycle description clean clean the build directory default complie, test, package, deploy site report, document
  17. 17. Clean Lifecycle Phase pre-clean clean post-clean command description mvn clean execute the clean lifecycle including pre-clean, clean, post-clean mvn clean:clean execute the clean phase of the clean lifecycle
  18. 18. Default Lifecycle Phase Phase validate test generate-sources prepare-package process-sources package generate-resources pre-integration-test process-resources verify compile install generate-test-sources deploy process-test-sources generate-test-resources process-test-resources test-compile
  19. 19. Default Lifecycle command description mvn validate execute validate phase mvn compile execute from validate phase to compile phase mvn test execute from validate phase to test phase mvn install execute fomr validate phase to install phase
  20. 20. Package-specific Lifecycle (JAR) Lifecycle Phase Goal <pluging>:<goal> process-resource resources:resources compile compiler:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package jar:jar install install:install deploy deploy:deploy
  21. 21. Package-specific Lifecycle (POM) Lifecycle Phase Goal <pluging>:<goal> package site:attach-descriptor install install:install deploy deploy:deploy
  22. 22. Test • Run as JUNIT test • Variables – testSourceDirectory • src/test/java – testOutputdirectory • target/test-classes – result • target/surefire-reports • Includsion – Java filenames that start with "Test" • **/Test*.java – Java filenames that end with "Test" • **/*Test.java – Java filenames that end with "TestCase" • **/*TestCase.java
  23. 23. Test mvn -Dtest=TestCircle test run test on TestCircle class mvn -Dtest=TestSquare,TestCi*le test run multiple names/patterns, separated by commas mvn -Dtest=TestCircle#mytest test mvn -Dtest=TestCircle#test* test mvn -Dtest=TestCircle#testOne+testTwo test run test on multiple methods mvn install -Dmaven.test.skip=true skip test
  24. 24. Package-specific Lifecycle (Maven Plugin) Lifecycle Phase Goal <pluging>:<goal> generate-resource plugin:descriptor process-resources resources:resources compile compiler:compile process-test-resource resources:testResources test-compile compile:testCompile test surefire:test package jar:jar, plugin:addPluginArtifactMetaData deploy deploy:deploy
  25. 25. Process Resource copy from ${basedir}/src/main/resource to ${project.build.outputDirector} (${basedir}/target/classes by default)
  26. 26. Compile <project> ... <build> ... <plugins> <plugin> <artifactId>maven-compiler-plugin</aftifactId> <configuration> <source>1.6</source> <target>1.5</tartet> </configuratin> </plugin> </plugins> </build> </project> source java 1.6 vm java 1.5
  27. 27. Compile <project> ... <build> ... <sourceDirectory>src/java</sourceDirectory> <outputDirectory>classes</outputDirectory> </build> </project>
  28. 28. Plugin <project> ... <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project> Configure variable used by plugin "Maven" is really just a core framework for a collection of Maven Plugins
  29. 29. <project> ... <build> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.2</version> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <revisionOnScmFailure>VersionUnknown</revisionOnScmFailure> </configuration> <executions> <execution> <goals> <goal>create</goal> </goals> <phase>validate</phase> </execution> </executions> </plugin> </plugins> </build> </project> execute the plugin goal create in the validate phase
  30. 30. <project> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> ...... </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> </build> </project> use pluginManagement to management plugin configuration
  31. 31. Profile • Per Project – pom.xml • Per User – %USER_HOME%/.m2/settings.xml • Global – %M2_HOME%/config/settings.xml mvn groupId:artifactId:goal -P profile-1,profile-2
  32. 32. Profile for Development <profile> <id>development</id> <properties> <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass> <db.connectionURL>jdbc:oracle:thin:@127.0.0.1:1521:XE</db.connectionURL> <db.username>devuser</db.username> <db.password>devpassword</db.password> <logo.image>mylogo.png</logo.image> </properties> </profile> mvn groupId:artifactId:goal -P development
  33. 33. Profile for Production <profile> <id>production</id> <properties> <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass> <db.connectionURL>jdbc:oracle:thin:@10.0.1.14:1521:APPS</db.connectionURL> <db.username>productionuser</db.username> <db.password>productionpassword</db.password> <logo.image>production_logo.png</logo.image> </properties> </profile> mvn groupId:artifactId:goal -P production
  34. 34. Resource Filtering <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> Enable resource filtering in specific directory maven variable in files in the specific directory will be replaced <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> add plugin configure resource filtering
  35. 35. Resource Filtering database.jdbc.driverClass=${db.driverClass} database.jdbc.connectionURL=${db.connectionURL} database.jdbc.username=${db.username} database.jdbc.password=${db.password} model.application.name=MyWebApp model.stylesheet=/mywebapp.css database.jdbc.driverClass=oracle.jdbc.driver.OracleDriver database.jdbc.connectionURL=jdbc:oracle:thin:@127.0.0.1:1521:XE database.jdbc.username=myusername database.jdbc.password=mypassword model.application.name=MyWebApp model.stylesheet=/mywebapp.css db.properties source db.properties source after resource filtering
  36. 36. Profile Activation <profiles> <profile> <id>appserverConfig-dev</id> <activation> <property> <name>env</name> <value>dev</value> </property> </activation> <properties> <server.home>/appserver</server.home> </properties> </profile> <profiles> <profiles> <profile> <id>appserverConfig-dev-2</id> <activation> <property> <name>env</name> <value>dev-2</value> </property> </activation> <properties> <server.home>/appserver2</server.home> </properties> </profile> <profiles> actived if env = dev actived if env = dev2
  37. 37. Source Code Management (SCM) Use SCM to update and checkin code <project> ... <scm> <connection>scm:svn:http://somerepository.com/svn_repo/trunk</connection> <developerConnection>scm:svn:https://somerepository.com/svn_repo/trunk</developerConnection> <url>http://somerepository.com/view.cvs</url> </scm> ... </project> mvn -Dmessage="<commit_log_here>" scm:checkin mvn scm:update check in code update
  38. 38. Name Value -Dmaven.test.skip=true skip test -o work offline, skip download pom -P <args> configurate active profile -X enable debug output -q quiet output, only show errors -npu no pluging updates help:active-profiles help:effective-pom output effective pom help:effective-setting output effective setting help:describe describe plugin, mvn help:describe -Dplugin=help

×