Skip to content

Commit 953f412

Browse files
authored
[MEDP-964] unconditionally ignore dependencies known to be loaded by reflection (#1492)
* unconditionally ignore dependencies known to be loaded by reflection * docs
1 parent 88585aa commit 953f412

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public abstract class AbstractAnalyzeMojo extends AbstractMojo {
7878
private boolean verbose;
7979

8080
/**
81-
* Ignore Runtime/Provided/Test/System scopes for unused dependency analysis.
81+
* Ignore runtime/provided/test/system scopes for unused dependency analysis.
8282
* <p>
8383
* <code><b>Non-test scoped</b></code> list will be not affected.
8484
*/
8585
@Parameter(property = "ignoreNonCompile", defaultValue = "false")
8686
private boolean ignoreNonCompile;
8787

8888
/**
89-
* Ignore Runtime scope for unused dependency analysis.
89+
* Ignore runtime scope for unused dependency analysis.
9090
*
9191
* @since 3.2.0
9292
*/
@@ -211,13 +211,18 @@ public abstract class AbstractAnalyzeMojo extends AbstractMojo {
211211
* segment is treated as an implicit wildcard. *
212212
* <p>
213213
* For example, <code>org.apache.*</code> matches all artifacts whose group id starts with
214-
* <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
214+
* <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> matches all snapshot artifacts.
215215
* </p>
216216
*
217+
* <p>Certain dependencies that are known to be used and loaded by reflection
218+
* are always ignored. This includes {@code org.slf4j:slf4j-simple::}.</p>
219+
*
217220
* @since 2.10
218221
*/
219-
@Parameter(defaultValue = "org.slf4j:slf4j-simple::")
220-
private String[] ignoredUnusedDeclaredDependencies;
222+
@Parameter
223+
private String[] ignoredUnusedDeclaredDependencies = new String[0];
224+
225+
private String[] unconditionallyIgnoredDeclaredDependencies = {"org.slf4j:slf4j-simple::"};
221226

222227
/**
223228
* List of dependencies that are ignored if they are in not test scope but are only used in test classes.
@@ -361,6 +366,7 @@ private boolean checkDependencies() throws MojoExecutionException {
361366

362367
ignoredUnusedDeclared.addAll(filterDependencies(unusedDeclared, ignoredDependencies));
363368
ignoredUnusedDeclared.addAll(filterDependencies(unusedDeclared, ignoredUnusedDeclaredDependencies));
369+
ignoredUnusedDeclared.addAll(filterDependencies(unusedDeclared, unconditionallyIgnoredDeclaredDependencies));
364370

365371
if (ignoreAllNonTestScoped) {
366372
ignoredNonTestScope.addAll(filterDependencies(nonTestScope, new String[] {"*"}));

src/site/apt/examples/exclude-dependencies-from-dependency-analysis.apt.vm

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ Exclude dependencies from dependency analysis
2828
A project's dependencies can be analyzed as part of the build process by binding the <<<dependency:analyze-only>>>
2929
goal to the lifecycle. By default, the analysis will be performed during the <<<verify>>> lifecycle phase.
3030

31-
In rare cases it is possible to have dependencies that are
32-
legitimate on the classpath but cause either "Declared but unused"
33-
or "Undeclared but used" warnings. The most common case is with jars
34-
that contain annotations and the byte code analysis is unable to
35-
determine whether a jar is actually required or not.
31+
It is possible to have necessary dependencies on the classpath that
32+
cause either "Declared but unused" or "Undeclared but used" warnings.
33+
One common cause of byte code analysis being unable to
34+
determine whether a jar is required are annotations with
35+
source retention. Another common cause is
36+
a class that is loaded by reflection at runtime.
3637

37-
The plugin can then be configured to ignore dependencies that are
38-
"declared but unused", "undeclared but used", and "non-test scoped"
39-
in selected list or in all simultaneously.
38+
The dependency plugin does not warn about a few common dependencies
39+
where its analysis is known to be unreliable, most notably SLF4J.
4040

41+
If you encounter other false positives, you can configure the plugin to ignore particular
42+
dependencies that are "declared but unused", "undeclared but used", and "non-test scoped".
4143
See the following POM configuration for an example:
4244

4345
+---+

0 commit comments

Comments
 (0)