vpiTriumph
vpiTriumph

Reputation: 3166

Suppress Maven Dependency Plugin's "Unused declared dependencies found" warnings

The maven-dependency-plugin identifies what it believes to be unused dependencies when you compile by producing warnings at compile time.

[WARNING] Unused declared dependencies found:
[WARNING]    org.foo:bar-api:jar:1.7.5:compile

In some cases this message is a false positive and the dependency is required transitively.

Question: How can I identify in my pom.xml that this is the case?

Upvotes: 23

Views: 32497

Answers (6)

Martin Miklovič
Martin Miklovič

Reputation: 71

This message can occur when you are not using dependency in compile time but in runtime. You can do following:

<dependency>
   <groupId>org.foo</groupId>
   <artifactId>bar-api</artifactId>
   <version>1.7.5</version>
   <scope>runtime</scope>
</dependency>

Upvotes: 6

Manuel Romeiro
Manuel Romeiro

Reputation: 1061

Since maven-dependency-plugin version 2.6 you can use usedDependencies tag to force dependencies as used.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <usedDependencies>
            <dependency>groupId:artifactId</dependency>
        </usedDependencies>
    </configuration>
</plugin>

Upvotes: 2

user9130454
user9130454

Reputation: 41

That was nearly what i was looking for, but i guess you specify that a little more like:

<execution>
  <goals>
     <goal>analyze-only</goal>
  </goals>
  <configuration>
  <failOnWarning>true</failOnWarning>
  <ignoredUnusedDeclaredDependencies>
      <ignoredUnusedDeclaredDependency>org.reflections:reflections:*</ignoredUnusedDeclaredDependency>
  </ignoredUnusedDeclaredDependencies>
  <ignoredUsedUndeclaredDependencies>
      <ignoredUsedUndeclaredDependency>junit:*:*</ignoredUsedUndeclaredDependency>
  </ignoredUsedUndeclaredDependencies>
  <ignoreNonCompile>false</ignoreNonCompile>
  <outputXML>true</outputXML>
  </configuration>
 </execution>

So this does nearly the same but is more specific on which kind of dependencies should be ignored

Upvotes: 4

supersingh05
supersingh05

Reputation: 21

try using provided scope

provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

Upvotes: 1

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

You should configure in your pom the ignoredDependencies element:

List of dependencies that will be ignored. Any dependency on this list will be excluded from the "declared but unused" and the "used but undeclared" list. The filter syntax is:

[groupId]:[artifactId]:[type]:[version]

where each pattern segment is optional and supports full and partial * wildcards. An empty pattern segment is treated as an implicit wildcard. *

As also specified by the official Exclude dependencies from dependency analysis. A sample configuration would be:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>analyze-dep</id>
                    <goals>
                        <goal>analyze-only</goal>
                    </goals>
                    <configuration>
                        <ignoredDependencies>
                            <ignoredDependency>org.foo:bar-api:jar:1.7.5</ignoredDependency>
                        </ignoredDependencies>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 26

npn_or_pnp
npn_or_pnp

Reputation: 515

You can use mvn dependency:tree to evaluate your dependencies.

Reference: https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

Upvotes: 2

Related Questions