Maven Dependency Plugin Example
In this example we are going to see some of the capabilities from the maven dependency plugin.
Maven is a build automation tool used mainly for java projects from apache.
You can access to the maven dependency plugin here.
We are going to see some examples of the capabilities of the maven dependency plugin.
For this example we use the following technologies:
- MAC OSX
- Eclipse Mars.1
- Maven3
- JDK 1.8.0_65 64bits
- Junit 4.12
- Maven dependency plugin 2.10
1. Introduction
The maven dependency plugin provides the capability to manipulate artifacts besides some other capabilities like analyze the project and search unused dependencies. We can call to mvn dependency:analyze
for example and we don´t have to define the maven dependency plugin inside pom.xml
it will be downloaded and executed when maven needs it.
Despite that, we can define it inside pom.xml
in order to control some features.
The maven dependency plugin has severals goals defined (from plugin´s page):
dependency:analyze
analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.dependency:analyze-dep-mgt
analyzes your projects dependencies and lists mismatches between resolved dependencies and those listed in your dependencyManagement section.dependency:analyze-only
is the same as analyze, but is meant to be bound in a pom. It does not fork the build and execute test-compile.dependency:analyze-report
analyzes the dependencies of this project and produces a report that summarises which are: used and declared; used and undeclared; unused and declared.dependency:analyze-duplicate
analyzes the and tags in the pom.xml and determines the duplicate declared dependencies.dependency:build-classpath
tells Maven to output the path of the dependencies from the local repository in a classpath format to be used in java -cp. The classpath file may also be attached and installed/deployed along with the main artifact.dependency:copy
takes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don’t exist in either the local repository or the reactor.dependency:copy-dependencies
takes the list of project direct dependencies and optionally transitive dependencies and copies them to a specified location, stripping the version if desired. This goal can also be run from the command line.dependency:display-ancestors
displays all ancestor POMs of the project. This may be useful in a continuous integration system where you want to know all parent poms of the project. This goal can also be run from the command line.dependency:get resolves
a single artifact, eventually transitively, from a specified remote repository.dependency:go-offline
tells Maven to resolve everything this project is dependent on (dependencies, plugins, reports) in preparation for going offline.dependency:list
alias for resolve that lists the dependencies for this project.dependency:list-repositories
displays all project dependencies and then lists the repositories used.dependency:properties
set a property for each project dependency containing the to the artifact on the file system.dependency:purge-local-repository
tells Maven to clear dependency artifact files out of the local repository, and optionally re-resolve them.dependency:resolve
tells Maven to resolve all dependencies and displays the version.dependency:resolve-plugins
tells Maven to resolve plugins and their dependencies.dependency:sources
tells Maven to resolve all dependencies and their source attachments, and displays the version.dependency:tree
displays the dependency tree for this project.dependency:unpack
like copy but unpacks.dependency:unpack-dependencies
like copy-dependencies but unpacks.
Now, we are going to see some of the capabilities in action with some examples.
2. Example project
For this example, we are going to use a java project with maven nature that will be packaged as a jar file. Eclipse Mars comes with maven support out of the box, so you don´t have to install anything. Our project will look like this
At this point, we have an empty maven project. We are going to define the maven dependency plugin inside pom.xml
in order to test the plugin capabilities.
The pom.xml
will look like this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-dependency-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven dependency :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> </plugin> </plugins> </build> </project>
The project has a dummy class, and two dependencies: spring-core
and junit
(test scoped).
3. See dependencies tree
The maven dependency plugin allow us to show the dependencies as a tree. You can see an example in the following pom.xml
:
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-dependency-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven dependency :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>tree</id> <phase>generate-sources</phase> <goals> <goal>tree</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Note you can add the org.eclipse.m2e:lifecycle-mapping
plugin in order to avoid eclipse errors, to do this put the cursor above the error mark over execution in the plugin definition and choose the ‘Permanently mark goal tree in pom.xml as ignored in Eclipse build’ option as you can see in the imagen below
Do this for all the situations you need. This operation will add some code to our pom.xml
at the end.
You can run the plugin with the dependency:tree -Doutput=./file.graphml -DoutputType=graphml
command, you will see a file at the root project folder with the graph content called file.graphml
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven dependency :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ maven-dependency-plugin-example --- [WARNING] The parameter output is deprecated. Use outputFile instead. [INFO] Wrote dependency tree to: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven dependency plugin/file.graphml [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.113 s [INFO] Finished at: 2015-12-20T20:16:49+01:00 [INFO] Final Memory: 11M/309M [INFO] ------------------------------------------------------------------------
You can use some other formats and you can see the tree in the output console rather than in a file, like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven dependency :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ maven-dependency-plugin-example --- [INFO] com.javacodegeeks.examples:maven-dependency-plugin-example:jar:1.0.0-SNAPSHOT [INFO] +- junit:junit:jar:4.12:compile [INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:compile [INFO] \- org.springframework:spring-core:jar:4.2.4.RELEASE:compile [INFO] \- commons-logging:commons-logging:jar:1.2:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.023 s [INFO] Finished at: 2015-12-20T20:11:22+01:00 [INFO] Final Memory: 11M/309M [INFO] ------------------------------------------------------------------------
4. Build classpath
Another interesting maven dependency plugin feature is the capability to build the project classpath as a string
The following pom.xml
shows an example:
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-dependency-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven dependency :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>build-classpath</id> <phase>generate-sources</phase> <goals> <goal>build-classpath</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
You can run the plugin with the mvn generate-sources
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven dependency :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:build-classpath (build-classpath) @ maven-dependency-plugin-example --- [INFO] Dependencies classpath: /Users/fhernandez/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/fhernandez/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/fhernandez/.m2/repository/org/springframework/spring-core/4.2.4.RELEASE/spring-core-4.2.4.RELEASE.jar:/Users/fhernandez/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.107 s [INFO] Finished at: 2015-12-20T20:46:28+01:00 [INFO] Final Memory: 11M/309M [INFO] ------------------------------------------------------------------------
5. Other features
As we had seen this plugin have several goals and we have saw a few examples, you can see here the usage of all other features.
6. Conclusions
As you have seen with this example, the maven dependency plugin allow you to do several things in order to fit your dependency management requirements.
7. Download the eclipse project
This was an example about Maven dependency plugin.
You can download the full source code of this example here: maven-dependency-plugin-example.zip