# Difference between revisions of "Maven"

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

### 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

### 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


#### 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).

### 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>

<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<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

#### 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.

### 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.

## Tutorials

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