Maven

From BC$ MobileTV Wiki
Revision as of 13:23, 14 August 2021 by Bcmoney (Talk | contribs)

Jump to: navigation, search
Apache Software Foundation (ASF) logo
Apache Maven project

Maven is a dependency management system and build tool for rapid Java-based application development with rich features to streamline development and SCM. Maven is a Yiddish word which means "accumulator of knowledge". The Maven Project started out as an attempt to simplify build processes in a project with multiple Ant build scripts. It was proposed that a standard way to build projects, clearly define what a project consists of, easily publish project information and share JARs across several different projects would greatly simplify and lend efficiency and speed to the Java development process.


Lifecycle

[1]

Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run
  • verify - run any checks to verify the package is valid and meets quality criteria
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

  • clean - cleans up artifacts created by prior builds
  • site - generates site documentation for this project

[2][3][4]

Goals

Build Commands

The following are some useful commands (don't forget to remove "mvn" before adding a command to a Run Configuration in your IDE of choice, the plugin will automatically call Maven).

Standard full clean & install (with complete maven repository building of dependencies via POM dependency list):

mvn clean install -PautoInstallPackage

Clean the target directory, build and explode out a WAR file, run it in an embedded Jetty server (should be configured properly in pom.xml).

mvn clean war:exploded jetty:run -e

Run unit tests:

mvn test

Package a runnable JAR, WAR or EAR:

mvn package

Build a new snapshot and release:

mvn -U deploy

An interesting thing to note is that phases and goals may be executed in sequence:

mvn clean dependency:copy-dependencies package

[5]


Troubleshooting

Search for JARs in your local Maven repository (local cache):

file ~/.m2/repository/org/jsoup/jsoup/1.10.3/jsoup-1.10.3.jar

Given a specific JAR, search contents to find a specific class (for debugging any "ClassNotFoundException" and similar issues):

jar -tf ~/.m2/repository/org/jsoup/jsoup/1.10.3/jsoup-1.10.3.jar | grep org/jsoup/parser/Parser


Profiles

Build Profiles are used to set one or more distinct sets of properties and/or build commands (for instance Local Dev .vs. Test Environment .vs. Prod Environment settings).

[6] [7]

[8] [9]


POM

In Maven, the Project Object Model (POM) is the term given to the XML-based definition of a project (similar to DOAP), which focuses on providing a listing of Dependencies (libraries, supporting tools, etc), where to download or obtain those Dependencies, and how to use those dependencies together with the code contained in the software project in order to successfully perform a Build. It also provides a linkage to Defect Management (aka. Issue Management) & Testing tools. The following is an empty template for the syntax of the POM file.

 <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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
   <dependencies>
     <dependency>
       <groupId>...</groupId>
       <artifactId>...</artifactId>
       <version>...</version>
     </dependency>
   </dependencies>
   <parent>...</parent>
   <dependencyManagement>...</dependencyManagement>
   <modules>...</modules>
   <properties>...</properties>

  <!-- Build Settings -->
   <build>...</build>
   <reporting>...</reporting>

  <!-- Additional 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>
 </project>


Dependency Tree

You can view your dependency tree by running the following from command-line:

mvn dependency:tree

For larger apps or heavier more complex Maven configurations, you may want to pipe the output to a file to open in a text editor as it could be hard to read from a console/terminal:

mvn dependency:tree -Dverbose >> C:/APPS/dependencies.txt


Find Out-of-Date Plugins

You can list Maven plugins that are outdated using the following command:

mvn versions:display-plugin-updates
Find Out-of-Date Dependencies

You can list libraries that are both out-of-date (it will also differentiate those that "should" be able to be upgraded safely, based on Semantic Versioning) using the following command:

mvn versions:display-dependency-updates



Tools

Plugins

Releases

Versions

SCM

Properties

[10]

Exec

[11] [12]

Assembly

Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files.

[13] [14] [15]

Continuous Integration

Continuous Integration

Static Code Analysis

[16] [17] [18] [19] [20] [21] [22]

Checkstyle
PMD
findbugs
spotbugs

jUnit

JaCoCo

SoapUI

Tomcat

Config processor

Castor

Exec (command-line)

CloudTools


Language Extensions

While just about any program can be run remotely in a cloud environment via the CloudTools plugin, or by calling a local program from the command-line via the Exec plugin, there are still instances when you want to work more closely with a given programming language's specific tools such as compilers, testing suites, IDEs, etc. You can accomplish this by using one of the many Language-specific Maven extensions.

PHP

Python

Flex

C#

C/C++

RubyScript




Resources

[33] [34] [35]


Tutorials

[47][48] [49] [50]

https://stackoverflow.com/questions/13708738/how-to-get-a-command-line-property-to-overwrite-a-maven-property?noredirect=1&lq=1

[63]

[65] [66] [67]

[68] [69] [70] [71]


External Links


References

  1. Setting Up Your Project to Use the Build Lifecycle: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#setting-up-your-project-to-use-the-build-lifecycle
  2. Maven in 5 Minutes: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
  3. Tutorial -- Build a JAR file with Maven in 5 minutes: https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes
  4. : https://stackoverflow.com/questions/3402806/what-does-the-name-snapshot-in-jar-mean-whats-wrong-with-snapshot-jars
  5. Maven build debug in Eclipse: https://stackoverflow.com/questions/12598261/maven-build-debug-in-eclipse/18616075#18616075
  6. Guide to Maven Profiles: https://www.baeldung.com/maven-profiles
  7. Setting default values for custom Maven 2 properties: https://stackoverflow.com/questions/899274/setting-default-values-for-custom-maven-2-properties
  8. Active by default Maven profile and other profiles: greyfocus.com/2015/06/activebydefault-maven-other-profiles/
  9. How to keep Maven profiles which are activeByDefault active even if another profile gets activated?: https://stackoverflow.com/questions/5309379/how-to-keep-maven-profiles-which-are-activebydefault-active-even-if-another-prof
  10. Read FILE properties via maven-properties plugin: https://www.mojohaus.org/properties-maven-plugin/read-project-properties-mojo.html
  11. mvn exec plugin -- Usage: https://www.mojohaus.org/exec-maven-plugin/usage.html (valid
  12. mvn exec plugin -- Changing the classpath scope when running Java programs: https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-or-java-change-classpath-scope.html
  13. Apache Mache Assembly Plugin - usage: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
  14. Apache Mache Assembly Plugin - POM.xml config examples: https://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html
  15. assembly:single - assemble an application bundle or distribution from an assembly descriptor.: https://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html#skipAssembly
  16. Maven -- Unable to locate Source XRef to link to: https://stackoverflow.com/questions/12038238/unable-to-locate-source-xref-to-link-to
  17. SCA reporting with "maven-jxr-plugin": https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jxr-plugin | DOCS - JXR plugin usage
  18. Better site documentation with Maven (Part V) - static code analysis with PMD: https://softwareriot.wordpress.com/2017/09/20/better-site-documentation-with-maven-part-v-static-code-analysis-with-pmd/
  19. OpenHAB's excellent example of a Static Code Analysis Tools is a Maven plugin that executes the Maven plugins for SpotBugs, Checkstyle and PMD and generates a merged .html report: https://github.com/openhab/static-code-analysis/blob/main/docs/maven-plugin.md
  20. Maven PMD - Analyzing JSP Code: https://maven.apache.org/plugins/maven-pmd-plugin/examples/jspReport.html
  21. Maven PMD - Analyzing JavaScript Code: https://maven.apache.org/plugins/maven-pmd-plugin/examples/javascriptReport.html
  22. The Maven build task now supports PMD analysis out of the box: https://devblogs.microsoft.com/devops/the-maven-build-task-now-supports-pmd-analysis-out-of-the-box/
  23. Maven – PMD example: https://www.mkyong.com/maven/maven-pmd-example/
  24. Analyzing JavaScript Code: https://maven.apache.org/plugins/maven-pmd-plugin/examples/javascriptReport.html
  25. Introduction to FindBugs: https://www.baeldung.com/intro-to-findbugs
  26. FindBugs Maven Plugin tutorial: https://www.petrikainulainen.net/programming/maven/findbugs-maven-plugin-tutorial/
  27. Native C/C++ Maven Plugin (usage):http://mojo.codehaus.org/maven-native/native-maven-plugin/usage.html
  28. How do I use Maven through a proxy?: https://stackoverflow.com/questions/1251192/how-do-i-use-maven-through-a-proxy
  29. Maven -- Versions plugin - Changing the project version: http://www.mojohaus.org/versions-maven-plugin/examples/set.html
  30. Maven -- Versions plugin - Advancing dependency versions: http://www.mojohaus.org/versions-maven-plugin/examples/advancing-dependency-versions.html
  31. Maven -- Versions plugin - Replacing -SNAPSHOT versions with their corresponding releases: http://www.mojohaus.org/versions-maven-plugin/examples/use-releases.html
  32. Robustly enforcing a minimum Maven version for your project: http://blog.peterlynch.ca/2009/12/robustly-enforcing-minimum-maven.html
  33. Creating Profile Specific Configuration Files With Maven: https://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/
  34. Build profiles -- (environment-specific) properties filtering example "pom.xml": https://github.com/pkainulainen/maven-examples/blob/master/properties-filtering/pom.xml
  35. Maven - How do I activate a profile from command line?: https://stackoverflow.com/questions/30927064/maven-how-do-i-activate-a-profile-from-command-line
  36. Where is Maven' settings.xml located on Mac os?: https://stackoverflow.com/questions/3792842/where-is-maven-settings-xml-located-on-mac-os
  37. How to Install Maven on macOS using Homebrew: https://www.youdriveai.com/how-to-install-maven-on-macos-using-homebrew
  38. Install Maven on Mac: https://java2blog.com/install-maven-on-mac/
  39. How to install Maven on Mac OSX: https://mkyong.com/maven/install-maven-on-mac-osx/
  40. How to Install Maven on Mac OS: https://www.journaldev.com/2348/install-maven-mac-os
  41. How to convert a Maven pom.xml into an Ivy ivy.xml file : http://stefan222devel.blogspot.com/2010/01/how-to-convert-maven-pomxml-into-ivy.html
  42. Convert Maven POM dependencies to ivy.xml file: http://stackoverflow.com/questions/3715811/maven-pom-dependencies-to-ivy-xml-file
  43. m2eclipse wont download maven artifacts: http://stackoverflow.com/questions/3963439/m2eclipse-wont-download-maven-artifacts
  44. Unable to download jars using M2Eclipse (0.10.0, using Maven 3): http://stackoverflow.com/questions/3019811/unable-to-download-jars-using-m2eclipse-0-10-0-using-maven-3/
  45. Code Sharing (amongst several team members)? Use Maven: https://pettermahlen.com/2010/05/01/code-sharing-use-maven/
  46. Maven & Jenkins with Selenium: Complete Tutorial: https://www.guru99.com/maven-jenkins-with-selenium-complete-tutorial.html
  47. NetBeans Platform Quick Start Using Maven: http://platform.netbeans.org/tutorials/nbm-maven-quickstart.html?intcmp=925655
  48. Maven 3 support in NetBeans IDE 7.0+: http://netbeans.dzone.com/articles/maven-3-support-in-netbeans-ide
  49. Integrating Maven with IBM Rational Application Developer (RAD) 7.5: http://pamirerdem.blogspot.ca/2010/07/integrating-maven-with-ibm-rational.html
  50. Hello World Maven on the NetBeans RCP: http://netbeans.dzone.com/hello-world-maven-netbeans-rcp
  51. NetBeans Platform Maven Command Line Tutorial: http://platform.netbeans.org/tutorials/nbm-maven-commandline.html?intcmp=925655
  52. NetBeans Platform Quick Start Using Maven: https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html
  53. Creating an Enterprise Application Using Maven
  54. NB - Convert a project to Maven?: https://forums.netbeans.org/topic55932.html
  55. Converting a Netbeans project to a Maven enabled project: https://stackoverflow.com/questions/5495213/converting-a-netbeans-project-to-a-maven-enabled-project
  56. How to Create Web Application using Maven and Netbeans: https://javapointers.com/tutorial/create-web-application-using-maven-and-netbeans/
  57. Add JDBC in Netbeans Maven Project: https://stackoverflow.com/questions/30947905/add-jdbc-in-netbeans-maven-project
  58. NetBeanws -- Testing a Maven Enterprise Application: https://netbeans.org/kb/docs/javaee/maven-entapp-testing.html
  59. Maven – How to skip unit test: https://www.mkyong.com/maven/how-to-skip-maven-unit-test/
  60. Difference between -Dmaven.test.skip=true and -DskipTests in Maven: https://memorynotfound.com/maven-skip-unit-tests/
  61. Maven skip tests (explained): https://stackoverflow.com/questions/24727536/maven-skip-tests
  62. maven profiles - load config.properties based on environment : http://maven.40175.n5.nabble.com/maven-profiles-load-config-properties-based-on-environment-td5752105.html
  63. Best practices for copying files with Maven: https://stackoverflow.com/questions/586202/best-practices-for-copying-files-with-maven
  64. Maven2 -- Missing artifact but JARs are in place: https://stackoverflow.com/questions/6111408/maven2-missing-artifact-but-jars-are-in-place
  65. Maven build script for replacement of text in web.xml (and others): https://blog.giantgeek.com/?p=1377
  66. Using Maven Replacer Plugin to replace strings from a properties file: https://crushedbeans.wordpress.com/2013/06/08/using-maver-replacer-plugin-to-replace-strings-from-a-properties-file/
  67. Maven replacer -- replacement value containing dollar signs: https://stackoverflow.com/questions/33667378/maven-replacer-replacement-value-containing-dollar-signs
  68. Understanding Maven Version Numbers: https://docs.oracle.com/middleware/1212/core/MAVEN/maven_version.htm#MAVEN8855
  69. Release vs. Snapshot repositories: http://www.scmgalaxy.com/tutorials/release-vs-snapshot-repositories/
  70. How to Use Snapshot of Library in Maven Project: https://dzone.com/articles/how-to-use-snapshot-of-library-in-maven-project
  71. Maven Snapshot Repository vs Release Repository: https://stackoverflow.com/questions/275555/maven-snapshot-repository-vs-release-repository
  72. m2e lifecycle-mapping not found: https://stackoverflow.com/questions/7409823/m2e-lifecycle-mapping-not-found/7423909
  73. Getting parent directory of ${basedir} from maven: https://stackoverflow.com/questions/19511785/getting-parent-directory-of-basedir-from-maven
  74. What is the purpose of mvnw and mvnw.cmd files?: https://stackoverflow.com/questions/38723833/what-is-the-purpose-of-mvnw-and-mvnw-cmd-files

See Also

Ant | Java | CI | Testing | SCM | Semantic Versioning