为什么需要添加工件JSR305才能使用Guava 14+? [英] Why do I need adding artifact JSR305 to use Guava 14+?

查看:204
本文介绍了为什么需要添加工件JSR305才能使用Guava 14+?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找有关stackoverflow的信息时,我看到了一个与我的问题类似的问题,但没有真正的答案

While looking for informations on stackoverflow, I have seen a question similar to mine, but with no real answer here.

我需要将Maven项目从 guava 11.0.2 迁移到 guava 14 或更高版本(我需要

I need migrating my maven project from guava 11.0.2 to guava 14 or higher (I need RangeSet). I updated my maven pom with the dependency:

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

然后我运行Maven构建,并得到以下错误:

I then run the maven build, and got this error:

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

我仔细看了一下, JSR305 提供了此批注,它依赖于 guava 11.0.2 ,如

I took a look closer, and this annotations is provided with JSR305, on which depends guava 11.0.2, as mvn repository reports it.

我发现奇怪的是,番石榴14 也依赖 JSR305 作为

What I find strange is that guava 14 also depends on JSR305 as mvn repository reports.

如果我将JSR依赖项添加到pom中,则编译运行良好:

If I add the JSR dependency to my pom, then the compilation just runs fine:

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

但是,如果番石榴已经依赖于它,为什么还要将这种依赖关系添加到我的pom中呢?这看起来更像是一种解决方法,而不是解决方案,我希望理解并使其变得干净.

But why would I have to add this dependency to my pom if guava already depends on it ? This looks more to a workaround than to a solution, and I would prefer to understand and make things clean.

感谢您的参与.

推荐答案

之所以需要将其添加为依赖项是因为Guava 14在其pom中定义了依赖项,如下所示:

The reason that you need to add it as a dependency is because Guava 14 defines the dependency in their pom as follows:

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

您的问题的重要部分是<scope>provided</scope>行.

The important part for your problem is the <scope>provided</scope> line.

在maven 网站中,他们声明了有关依赖项:

From the maven website they state the following with regards to provided dependencies:

已提供 : 这与compile非常相似,但表示您希望JDK或容器在运行时提供依赖项.例如,在为Java Enterprise Edition构建Web应用程序时,您将对Servlet API和相关Java EE API的依赖关系设置为范围provided,因为Web容器提供了这些类.此作用域仅在编译和测试类路径上可用,并且不可传递.

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.

所以基本上,因为番石榴已将其设置为provided依赖性,所以他们希望食用Guava的任何人都可以提供这种依赖性,而这正是您必须要做的.

So basically because Guava have set this as a provided dependency they expect whoever is consuming Guava to provide this dependency which is exactly what you have had to do.

在Guava 11.0.2中,它是一个普通的compile依赖项,因此您不必在自己的项目中提供它.

In Guava 11.0.2 it was a normal compile dependency, hence you didn't have to provide it in your own project.

更改是在Guava 13中进行的.摘自发行说明:

The change was made in Guava 13. From the release notes:

使用findbugs 2.0时,可以为findbugs提供一个依赖项来避免dep冲突.此更改的副作用是,依赖于Guava来免费"授予对JSR-305批注的访问权限的项目将中断,除非它们自己对该jar(或等效项)提供直接依赖.项目应该始终一直直接依赖于JSR-305(按照maven的最佳实践),但是这种更改使这应该成为必须.

这篇关于为什么需要添加工件JSR305才能使用Guava 14+?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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