Maven 中未使用/未声明的依赖项是什么?该怎么办?

发布于 2024-10-09 22:56:35 字数 870 浏览 0 评论 0原文

Maven dependency:analyze 抱怨我的项目中的依赖项。它如何确定哪些未使用、哪些未声明?我该怎么办?

示例:

$ mvn dependency:analyze 
...
[WARNING] Used undeclared dependencies found:
[WARNING]    org.slf4j:slf4j-api:jar:1.5.0:provided
[WARNING]    commons-logging:commons-logging:jar:1.1.1:compile
[WARNING]    commons-dbutils:commons-dbutils:jar:1.1-osgi:provided
[WARNING]    org.codehaus.jackson:jackson-core-asl:jar:1.6.1:compile

...
[WARNING] Unused declared dependencies found:
[WARNING]    commons-cli:commons-cli:jar:1.0:compile
[WARNING]    org.mortbay.jetty:servlet-api:jar:2.5-20081211:test
[WARNING]    org.apache.httpcomponents:httpclient:jar:4.0-alpha4:compile
[WARNING]    commons-collections:commons-collections:jar:3.2:provided
[WARNING]    javax.mail:mail:jar:1.4:provided

注意: 我的运行时容器中使用了许多这些依赖项,我按照提供的方式声明了它们,以避免在类路径上两次使用不同版本的相同库。

Maven dependency:analyze complains about the dependencies in my project. How does it determine which are unused and which are undeclared? What should I do about them?

Example:

$ mvn dependency:analyze 
...
[WARNING] Used undeclared dependencies found:
[WARNING]    org.slf4j:slf4j-api:jar:1.5.0:provided
[WARNING]    commons-logging:commons-logging:jar:1.1.1:compile
[WARNING]    commons-dbutils:commons-dbutils:jar:1.1-osgi:provided
[WARNING]    org.codehaus.jackson:jackson-core-asl:jar:1.6.1:compile

...
[WARNING] Unused declared dependencies found:
[WARNING]    commons-cli:commons-cli:jar:1.0:compile
[WARNING]    org.mortbay.jetty:servlet-api:jar:2.5-20081211:test
[WARNING]    org.apache.httpcomponents:httpclient:jar:4.0-alpha4:compile
[WARNING]    commons-collections:commons-collections:jar:3.2:provided
[WARNING]    javax.mail:mail:jar:1.4:provided

Note:
A lot of these dependencies are used in my runtime container and I declared them as provided to avoid having the same library on the classpath twice with different versions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狼性发作 2024-10-16 22:56:35

不知道 Maven 如何确定这一点。不需要解决由此报告的所有项目,但可以酌情使用此信息。

使用的未声明的依赖项是那些必需的,但尚未在项目中显式声明为依赖项的依赖项。然而,由于项目中其他依赖项的传递依赖,它们是可用的。显式声明这些依赖项是个好主意。这还允许您控制这些依赖项的版本(可能与运行时提供的版本匹配)。

至于未使用的已声明依赖项,最好删除它们。为什么要向您的项目添加不必要的依赖项?但是传递性无论如何都会带来这些,也许与您的运行时版本发生冲突。在这种情况下,您需要指定它们 - 主要是为了控制版本

顺便说一句,mvn dependency:tree 提供了项目的依赖关系树,这使您可以更好地了解每个依赖关系如何适合您的项目。

Not sure how Maven determines this. It is not required to address all the items reported by this, but this information can be used as appropriate.

Used undeclared dependencies are those which are required, but have not been explicitly declared as dependencies in your project. They are however available thanks to transitive dependency of other dependencies in your project. It is a good idea to explicitly declare these dependencies. This also allows you to control the version of these dependencies (perhaps matching the version provided by your runtime).

As for unused declared dependencies, it is a good idea to remove them. Why add unnecessary dependency to your project? But then transitivity can bring these in anyway, perhaps, conflicting with your runtime versions. In this case, you will need to specify them — essentially to control the version.

By the way, mvn dependency:tree gives the dependency tree of the project, which gives you a better perspective of how each dependency fits in in your project.

初见你 2024-10-16 22:56:35

答案是:

“它如何确定哪些未使用,哪些未声明?”。

Maven 使用 Object WebASM 框架来分析原始字节码。它会遍历所有类,然后构建这些引用的所有类的列表。就是这样。

至于该怎么做,我不建议删除“未使用的、声明的依赖项”,除非您绝对确定它们实际上未使用。

The answer to:

"How does it determine which are unused and which are undeclared?".

Maven uses Object WebASM framework that analyzes your raw bytecode. It goes through all your classes and then builds a list of all classes that these reference. That is the how.

As to what to do, I would not recommend removing the "unused, declared dependecies" unless you are absolutely sure they actually unused.

羅雙樹 2024-10-16 22:56:35

使用声明的依赖项
简而言之,它们是传递依赖项,您正在使用它们,但没有在 POM 文件中显式声明它们

在下图中,橙色的。
输入图像描述这里

提示:
最好在 POM 文件中声明它们,以便与您的第一级依赖项松散耦合,因此将来如果他们计划更改其实现并且不再使用这种传递依赖项,您的应用程序会安全的!

未使用已声明的依赖项
简而言之,它们是您在 POM 文件中声明它们的依赖项而不是在应用程序代码中使用它们

在下图中,红色的。
输入图像描述这里

提示:
从 POM 文件中删除它们是个好主意,因为它们没有被使用,并且保存应用程序工件的最终大小也可以避免任何开发人员错误地使用错误的类!

Used undeclared dependencies
Simply, they are the transitive dependencies which you are using them but WITHOUT declaring them explicitly inside your POM file.

In the below digram, the orange colored one.
enter image description here

Hint:
It is good idea to declare them inside your POM file to be loosly coupled against your first level dependencies, so in the future if they planned to change their implementation and not to use this transitive dependency anymore, your application will be safe!

Unused declared dependencies
Simply, they are the dependencies which you are declearing them inside your POM file WITHOUT using them in your application code.

In the below digram, the red colored one.
enter image description here

Hint:
It is good idea to remove them from your POM file, because they are not used and to save the final size of the application artifact also to avoid any developer from using wrong classes by mistake!

笨笨の傻瓜 2024-10-16 22:56:35

通过在 pom.xml 中添加ignoredUnusedDeclaredDependency可以轻松解决此问题

 <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>

This can be easily fixed with by adding ignoredUnusedDeclaredDependencies in pom.xml

 <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文