Maven中的未使用/未声明的依赖项是什么?他们怎么办? [英] What are unused/undeclared dependencies in Maven? What to do with them?

查看:162
本文介绍了Maven中的未使用/未声明的依赖项是什么?他们怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven 依赖关系:分析抱怨我的项目中的依赖关系。它如何确定哪些是未使用的,哪些是未声明的?我应该怎么做?



示例:

  $ mvn依赖关系:分析
...
[警告]发现使用未声明的依赖项:
[警告] org.slf4j:slf4j-api:jar:1.5.0:提供
[警告] commons-logging:commons-logging:jar:1.1.1:compile
[WARNING] commons-dbutils:commons-dbutils:jar:1.1-osgi:提供
[WARNING] org.codehaus.jackson :jackson-core-asl:jar:1.6.1:编译

...
[警告]发现未使用的声明依赖项:
[WARNING] commons-cli:commons- cli:jar:1.0:compile
[警告] 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:提供
[警告] javax.mail:mail:jar:1.4:提供

注意:
这些依赖关系很多都在我的运行时容器中使用,我声明它们是提供的,以避免有相同的类库中的库两次,具有不同的版本。

解决方案

不知道Maven如何决定这一点。不需要处理由此报告的所有项目,但可以适当使用此信息。



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



对于未使用的声明依赖关系,删除它们是一个好主意。为什么添加不必要的依赖项目?但是,传统性可以使这些无论如何,也许与您的运行时版本冲突。在这种情况下,您需要指定它们 - 基本上是为了控制版本



顺便说一句, code> mvn依赖:tree 给出项目的依赖关系树,这样可以更好地了解每个依赖关系如何适合你的项目。 >

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.

解决方案

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.

这篇关于Maven中的未使用/未声明的依赖项是什么?他们怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆