java – 为什么我需要添加神器JSR305才能使用Guava 14?

前端之家收集整理的这篇文章主要介绍了java – 为什么我需要添加神器JSR305才能使用Guava 14?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在查找stackoverflow的信息时,我看到了一个类似于我的问题,但没有真正的答案 here.

我需要将我的maven项目从番石榴11.0.2迁移到番石榴14或更高(我需要RangeSet).我用依赖更新了我的maven pom:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>14.0</version>
</dependency>

然后我运行maven构建,并得到此错误

[ERROR] xxx.java: cannot find symbol
[ERROR] symbol  : class Nonnull
[ERROR] location: package javax.annotation

我仔细观察了这个注释,JSR305提供了这个注释,其中依赖于guava 11.0.2,正如mvn repository报道的那样.

我觉得奇怪的是,番石榴14还依赖于JSR305作为mvn repository的报道.

如果我将JSR依赖项添加到我的pom,那么编译运行正常:

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>1.3.9</version>
    <scope>provided</scope>
</dependency>

但是,如果guava已经依赖它,为什么我必须将此依赖项添加到我的pom中?这看起来更像是一种解决方法,而不是解决方案,我更愿意理解并使事情变得清晰.

感谢您的参与.

解决方法

您需要将其添加为依赖项的原因是因为Guava 14在其pom中定义了依赖关系,如下所示:
<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>1.3.9</version>
    <scope>provided</scope>
</dependency>

您问题的重要部分是< scope>提供< / scope>线.

从maven website开始,他们就提供的依赖关系说明如下:

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.

所以基本上是因为Guava已将此设置为提供的依赖关系,他们希望任何使用Guava的人提供此依赖关系,这正是您必须要做的事情.

在Guava 11.0.2中,它是一个正常的编译依赖项,因此您不必在自己的项目中提供它.

改变是在番石榴13.从release notes

Made findbugs a provided dependency to avert dep conflicts when using findbugs 2.0. The side-effect of this change is that projects which relied upon Guava to grant access to the JSR-305 annotations “for free” will break unless they provide their own direct dependency on that jar (or an equivalent). Projects should always have been directly depending on JSR-305 (per maven best-practice),but this change makes that should into a must.

原文链接:https://www.f2er.com/java/124352.html

猜你在找的Java相关文章