mvn dependency:analyze 是如何工作的?

How does mvn dependency:analyze work?

有人可以告诉我 mvn dependency:analyze 是如何工作的吗?我的一个项目中 mvn dependency:analyze 的输出显示

[WARNING] Used undeclared dependencies found:
[WARNING]    org.apache.commons:commons-lang3:jar:3.4:compile
[WARNING]    com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
...
[WARNING] Unused declared dependencies found:
[WARNING]    org.springframework.boot:spring-boot-starter-test:jar:1.5.4.RELEASE:test
[WARNING]    org.springframework.restdocs:spring-restdocs-mockmvc:jar:1.1.3.RELEASE:test
[WARNING]    ch.qos.logback:logback-classic:jar:1.1.11:compile

谁能告诉我以下内容 -

Maven 版本 - 3.5.0

如果我没记错的话,maven使用WebASM框架来分析字节码并检查是否使用了lib。不要相信它,因为有时maven认为没有使用lib(依赖),但它是。

What does Used undeclared dependencies found denote? Does it mean that this is not declared in pom.xml dependencies but getting used in code and is included via some transitive dependencies?

完全正确!

Does Unused declared dependencies found check only for the dependencies declared in pom.xml or it checks transitive dependencies as well?

已声明的依赖项是在您的 POM 中声明的依赖项。因此该插件在其检查中不包括传递依赖项。

请注意,该插件默认执行字节码分析,这对于仅与常量或注释一起使用的依赖项存在问题。在某些情况下,这可能会导致虚假报告。有关详细信息,请参阅 FAQ

这可以通过在 pom.xml

中添加 ignoredUnusedDeclaredDependencies 轻松解决
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <ignoredUnusedDeclaredDependencies>
                    <ignoredUnusedDeclaredDependency>org.slf4j:slf4j-api</ignoredUnusedDeclaredDependency>
                </ignoredUnusedDeclaredDependencies>
            </configuration>
        </execution>
    </executions>
 </plugin>