java Gradle 中的“提供”依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30731084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:34:31  来源:igfitidea点击:

'Provided' dependency in Gradle

javagradledependency-management

提问by ps-aux

I have build.gradlein front of me and there are some dependencies declared as providedbut in documentationI do not see this dependency scope.

build.gradle面前有一些依赖项声明为provided但在文档中我没有看到这个依赖项范围。

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.2.4.RELEASE")
    ....

    provided 'backport-util-concurrent:backport-util-concurrent:3.1'
    provided 'org.javolution:javolution:5.5.1@jar
    ....
}

Is this provided by a plugin? If so how do I found out which plugin this belongs to?

这是由插件提供的吗?如果是这样,我如何找出它属于哪个插件?

What is the difference between providedand runtimedependency scope in Gradle?

Gradle 中的依赖范围providedruntime依赖范围有什么区别?

回答by Sazzadur Rahaman

What is providedscope?

什么是provided范围?

Suppose that a jaris needed to compile your code, but the jar is present in the production environment library collection. Then you don't need to package the jar with your project archives. To support this requirement, Maven has a scope named provided. If you declare any jar dependency as provided, then this jar will be present in your classpath during compilation but will not be packaged with your project archive.

假设需要 ajar来编译您的代码,但 jar 存在于生产环境库集合中。然后您不需要将 jar 与您的项目档案一起打包。为了支持这个要求,Maven 有一个名为provided. 如果您将任何 jar 依赖项声明为provided,则该 jar 将在编译期间出现在您的类路径中,但不会与您的项目存档一起打包。

providedscope is very useful, particularly in web applications. For example, servlet-api.jaris needed to be present in your classpath to compile your project, but you don't need this to package servlet-api.jarfile with your war. With providedscope one can achieve this requirement.

providedscope 非常有用,尤其是在 Web 应用程序中。例如,servlet-api.jar需要存在于您的类路径中以编译您的项目,但您不需要它来将servlet-api.jar文件与war. 有了provided范围,就可以实现这一要求。

There is no Scope defined in Gradle javaplugin named provided. Also not in waror androidplugins. If you want to use providedscope in your project, then you have to define it in your build.gradlefile. Following is the code snippet to declare providedscope in gradle:

Gradlejava插件中没有定义 Scope名为provided. 也不在warandroid插件中。如果你想provided在你的项目中使用范围,那么你必须在你的build.gradle文件中定义它。以下是provided在 gradle 中声明范围的代码片段:

configurations {
    provided
}

sourceSets {
    main { compileClasspath += configurations.provided }
}

Now, your second question:

现在,你的第二个问题:

What is the difference between provided and runtime dependency scope in Gradle?

Gradle 中提供的和运行时依赖范围有什么区别?

To answer this question first I will define compiledependency. compiledependencies are dependencies, those are necessary to compile your code. Now imagine that if your code uses a library named Xthen you must declare Xas your compile-time dependency. Also imagine that Xuses another library Yinternally, and you declared Yas your runtime dependency.

为了首先回答这个问题,我将定义compile依赖项。compile依赖项是依赖项,它们是编译代码所必需的。现在想象一下,如果您的代码使用名为的库,X那么您必须声明X为您的编译时依赖项。还可以想象它在内部X使用另一个库Y,并且您声明Y为您的运行时依赖项。

During compilation, Gradle will add Xinto your classpath but will not add Y. Since, Yis not required for compilation. But it will package both Xand Ywith your project archive since both Xand Yare necessary to run your project archive in the production environment. Generally, all the dependencies needed in the production environment are known as runtimedependency.

在编译期间,Gradle 会添加X到您的类路径中,但不会添加Y. 因为,Y不需要编译。但是,这两种包装XY使用,因为两个项目档案X,并Y为需要运行在生产环境中的项目归档。通常,生产环境中所需的所有依赖项都称为runtime依赖项。

In Gradle official documentation, it says that runtimedependency are "the dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.".

在 Gradle 官方文档中,它说runtime依赖是“生产类在运行时所需的依赖。默认情况下,还包括编译时的依赖。”。

Now, if you've read this far, then you already know that providedis a compiledependency that we don't want to be present in the runtimedependency (basically, we don't want it to package with the project archive).

现在,如果您已经读到这里,那么您已经知道这provided是一个compile我们不希望出现在runtime依赖项中的依赖项(基本上,我们不希望它与项目存档一起打包)。

Following is an illustration of providedand runtimescope. Here, compilerefers to the dependencies that are required to compile the project and non-compilerefers to the dependencies that are not required for project compilation.

以下是说明providedruntime范围。这里,compile指的是编译工程需要的依赖,指的是编译不需要non-compile的依赖。

回答by PaulNUK

As from gradle 2.12 you can use the compileOnly option.

从 gradle 2.12 开始,您可以使用 compileOnly 选项。

See

https://blog.gradle.org/introducing-compile-only-dependencies

https://blog.gradle.org/introducing-compile-only-dependencies

回答by gagarwa

For further clarification, as of the latest version, Gradle 5.5 has compileOnly (same as provided) and runtimeOnly options. The new default compile and runtime option is implementation.

为了进一步说明,从最新版本开始,Gradle 5.5 具有 compileOnly(与提供的相同)和 runtimeOnly 选项。新的默认编译和运行时选项是 implementation。