You can spot the difference between
just ignoring something and not throwing warnings (or errors, depending on plugin configuration) during analysis via ignoredUnusedDeclaredDependencies
and
actually telling Maven that the dependency is used even though the analyzer cannot detect it via usedDependencies
by running goal dependency:analyze-report
and looking the the report in target/dependency-analysis.html
: In the former case the ignored unused dependency will still be listed as "used undeclared". In the latter case the dependency will not be listed because you told Maven that it is actually used in your project.
When would you use either option?
If there is a dependency that is actually unused, but for whatever reason you wish to ignore it and not issue any warnings or errors for it, use ignoredUnusedDeclaredDependencies
. The report will still list it though, so you have a reminder to clean up eventually.
Dependency analysis is done based on byte code. So if you use an annotation library where some annotations have retention scope SOURCE
, Maven cannot detect annotation usage because the annotations are not contained in the compiled class files. But still they are being used. In this case you use usedDependencies
. Consequently and correctly, the so declared used dependencies will not appear as unused in the analysis report. Another use case is if you load classes via reflection and Maven Dependency Plugin is unable to detect it.
Here is a little sample POM. Just run mvn dependency:analyze-report
, check out the report HTML and then switch to the commented out option in order to see the difference.
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>de.scrum-master</groupId>
<artifactId>used-vs-ignoredUnused</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>
commons-collections:::
</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
<!--
<usedDependencies>
<usedDependency>
commons-collections:commons-collections
</usedDependency>
</usedDependencies>
-->
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
</project>