Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Learning Maven by Example

1.427 visualizaciones

Publicado el

Learning Maven by Example

Publicado en: Software
  • Inicia sesión para ver los comentarios

Learning Maven by Example

  1. 1. Learning Maven by example Hsi-Kai Wang
  2. 2. Outline • Introduction to Maven • Example 1 - simple case – build jar with command – build jar with maven • Example 2 - complex case – optimize POM • Maven Repository • Maven Project on Jenkins
  3. 3. Build Jar Flow submit sync send email deploy dependency
  4. 4. Introduction to Maven • What is maven – A more formal definition of Apache Maven: Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle. • Convention over Configuration – Maven concept is "Convention over Configuration". Maven provides default values for the project's configuration. Systems, libraries, and frameworks should assume reasonable defaults. Without requiring unnecessary configuration, systems should "just work" Ref: apche Maven
  5. 5. Installation • # install maven centos:~ # wget http://ftp.tc.edu.tw/pub/Apache/maven/maven- 3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz centos:~ # tar zxf apache-maven-3.3.9-bin.tar.gz -C /opt centos:~ # ln -s /opt/apache-maven-3.3.9 /opt/apache- maven • # setup environment variable centos:~ # export MAVEN_HOME=/opt/apache-maven centos:~ # export PATH=$PATH:$MAVEN_HOME/bin centos:~ # echo $JAVA_HOME centos:~ # mvn -v • # other maven options centos:~ # export MAVEN_OPTS="-Xms512m"
  6. 6. Example 1 - code & config mypackage/Hello.java log4j.properties mypackage/HelloTest.java
  7. 7. Example 1 – build, run & test • # compile centos:~ # javac -cp log4j-1.2.12.jar mypackage/Hello.java centos:~ # javac -cp ~/junit-4.10.jar:mypackage.jar mypackage/HelloTest.java • # package (create jar) centos:~ # jar cf mypackage.jar mypackage • # run centos:~ # java -cp log4j-1.2.12.jar:mypackage.jar - Dlog4j.configuration=file:///root/log4j.properties centos:~ # java -cp ~/junit-4.10.jar:mypackage.jar org.junit.runner.JUnitCore mypackage/HelloTest
  8. 8. Example 1 - maven folder structureproject folder test case folder application folder
  9. 9. Example 1 - pom.xml jar cf mypackage.jar mypackage javac -cp log4j-1.2.12.jar mypackage/Hello.java java -cp log4j-1.2.12.jar mypackage
  10. 10. Example 1 - maven command • # help centos:~/project # mvn help:describe -Dplugin=clean [- Dfull] • # compile centos:~/project # mvn clean compile • # run test case centos:~/project # mvn clean test [- Dmaven.test.failure.ignore=true] • # package centos:~/project # mvn clean package • # install module centos:~/project # mvn clean install [-DskipTests] • # run centos:~/project # mvn exec:java [- Dexec.mainClass=mypackage.Hello]
  11. 11. Example 1 - mvn compile & package
  12. 12. Maven Core Concept • Coordinates • Plugins and Goals • Lifecycle • Repositories • Dependency Management • Site Generation and Reporting
  13. 13. Coordinates coordinates description
  14. 14. Plugin and Goal • Maven is - at its heart - a plugin execution framework; all work is done by plugins(Plugin is a collection of one or more goals). Looking for a specific goal to execute? This page lists the core plugins and others. There are the build and the reporting plugins: – Build plugins will be executed during the build and they should be configured in the <build/> element from the POM. – Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element from the POM/
  15. 15. Lifecycle • A build lifecycle is an organized sequence of phases that exist to give order to a set of goals. Those goals are chosen and bound by the packaging type of the project being acted upon. There are three standard lifecycles in Maven: clean, default (sometimes called build) and site.
  16. 16. Command Syntax • # syntax 1 centos:~/project # mvn <coordinate>:<goal> centos:~/project # mvn org.apache.maven.plugin:maven-help- plugin:2.1:describe • # syntax 2 centos:~/project # mvn <plugin>:<goal> centos:~/project # mvn help:describe • # syntax 3 centos:~/project # mvn <phase> centos:~/project # mvn compile <groupId>:<artifactId>:<version>:<goal>
  17. 17. Default/Build lifecycle phase • Default – Handle project building and deployment validate compile test packagedeploy install
  18. 18. Lifecycle – clean and site phases • Clean – Cleaning target folder and generated file • Site – Generate documentation pre-clean clean post-clean pre-site site post-site site-deploy
  19. 19. Repositories /<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging>
  20. 20. Dependency Management • transitive dependency
  21. 21. Site Generation and Reporting
  22. 22. POM component • properties • dependencies • plugins • parent • modules • dependencyManagement • pluginManagement
  23. 23. POM component - properties • centos:~/project # mvn clean package • centos:~/project # mvn clean package - Dmypackage.version=1.2-SNAPSHOT
  24. 24. POM component - dependencies
  25. 25. POM component - plugins
  26. 26. Example 2 ref: Maven by Example
  27. 27. POM component - modules
  28. 28. POM component - parent
  29. 29. Optimize POM
  30. 30. POM component - dependencyManagement
  31. 31. Maven repository • Open Source: – Apache Archiva – JFrog Artifactory Open Source – Sonatype Nexus OSS • Commercial: – JFrog Artifactory Pro – Sonatype Nexus Pro Ref: Best Practice - Using a Repository Manager
  32. 32. Sonatype Maven repository - installation• # Setup firewall centos:~ # firewall-cmd --zone=public --add-port=8081/tcp – permanent centos:~ # firewall-cmd --zone=public --add-service=http – permanent centos:~ # firewall-cmd –reload • # Download & uncompress package http://www.sonatype.org/nexus/go/ • # Run centos:~ # /opt/nexus/bin/nexus start centos:~ # /opt/nexus/bin/nexus stop centos:~ # /opt/nexus/bin/nexus console • # Browser http://localhost:8081/nexus Administrator (admin/admin123) Depolyment (depolyment/depolyment123)
  33. 33. Hosted Repository - Nexus Hosted Repository Proxy Repository Virtual Repository Repository Group Snapshot Release
  34. 34. Hosted Repository - mvn deploy ~/.m2/settings.xml project/pom.xml
  35. 35. Proxy Repository - Nexus
  36. 36. Maven Repository – method 1 ~/.m2/settings.xml
  37. 37. Maven Repository – method 2 ~/.m2/settings.xml
  38. 38. Repository Group- Nexus
  39. 39. Maven Project on Jenkins (1)
  40. 40. Maven Project on Jenkins (2)
  41. 41. Maven Project on Jenkins (3)
  42. 42. reference • Apache Maven Project • Maven by Example • Maven Tutorial - TutorialsPoint

×