Maven - Dependency

About

dependency management in Maven.

Dependency are artifact where your software depends on.

Dependency are defined in the pom.xml, section dependencies.

Flow

When you build an application, Maven will search for dependencies in the local repository. If a stable version is not found there, it will search the remote repositories to retrieve this dependency. Then, it will copy it into the local repository, to make it available for the next builds.

Management

The dependency management section is a mechanism for:

  • centralizing dependency information.

Analyze

mvn dependency:analyze

Resolve

mvn dependency:resolve

Get

get

mvn dependency:get -Dartifact=group:name:version 

You can also specify a remote repository Url with -DremoteRepositories=Url

List

mvn dependency:list -DoutputAbsoluteArtifactFilename=true -DoutputFile=dependencies.txt

Missing

mvn dependency:tree

Exclude children

In case of dependency hell …

<dependency>
	<groupId>com.spotify</groupId>
	<artifactId>docker-client</artifactId>
	<version>5.0.2</version>
	<scope>test</scope>
	<exclusions>
	  <exclusion>
		<artifactId>guava</artifactId>
		<groupId>com.google.guava</groupId>
	  </exclusion>
	  <exclusion>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
	  </exclusion>
	</exclusions>
</dependency>

Configuration

Snapshot Fetch

According to snapshot default updatePolicy, maven will fetch the snapshot artifact from the repository on daily basis. See snapshot configuration

mvn clean install -U

where:

pom.xml

Dependency Property as defined in the pom.xml, section dependencies.

Scope

This element refers to the classpath of the task at hand (compiling and runtime, testing, etc.) as well as how to limit the transitivity of a dependency. There are five scopes available:

  • compile - (default). Compile dependencies are available in all classpaths. Furthermore, those dependencies are propagated to dependent projects.
  • provided - like compile, and indicates that you expect it to be provided at runtime by the JDK or a container.
  • runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test - this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
  • system - this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
    <scope>test</scope>
</dependency>

Type

Artifact's packaging type (defaults to jar)

<type>jar</type>

Maven - Package ??

Artifact's identifier

Optional

<optional>true</optional>

Install

For dependency that are not open source, the artifact must be installed in a not public repository.

For an installation on:

Example with an Oracle 's driver installation in the local maven repository as follows:

cd $ORACLE_HOME/jdbc/lib
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.2.0 -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true

Copy / download

Copy = Download dependency from the repository.

With the mvn command tool:

mvn dependency:copy-dependencies

Example of Usage: Dos bat file that download all jar dependency in a output directory, then call a main class

@echo off

if not exist target\dependencies (call mvn -B dependency:copy-dependencies -DoverWriteReleases=false -DoverWriteSnapshots=false -DoverWriteIfNewer=true -DoutputDirectory=target\dependencies)

java -Xmx1G -cp ".\target\dependencies\*" package.mainClass --verbose=true %*

Build dependency classpath

Build the class path

mvn dependency:build-classpath -Dmdep.outputFile=target/classpath.txt

Tree

Github

Support

Could not resolve dependencies for project

If maven can't resolve reactor module, this is because you need to install them first

mvn clean install

Documentation / Reference


Powered by ComboStrap