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
'Provided' dependency in Gradle
提问by ps-aux
I have build.gradle
in front of me and there are some dependencies declared as provided
but 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 provided
and runtime
dependency scope in Gradle?
Gradle 中的依赖范围provided
和runtime
依赖范围有什么区别?
回答by Sazzadur Rahaman
What is
provided
scope?
什么是
provided
范围?
Suppose that a jar
is 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 将在编译期间出现在您的类路径中,但不会与您的项目存档一起打包。
provided
scope is very useful, particularly in web applications. For example, servlet-api.jar
is needed to be present in your classpath to compile your project, but you don't need this to package servlet-api.jar
file with your war
. With provided
scope one can achieve this requirement.
provided
scope 非常有用,尤其是在 Web 应用程序中。例如,servlet-api.jar
需要存在于您的类路径中以编译您的项目,但您不需要它来将servlet-api.jar
文件与war
. 有了provided
范围,就可以实现这一要求。
There is no Scope defined in Gradle java
plugin named provided
. Also not in war
or android
plugins. If you want to use provided
scope in your project, then you have to define it in your build.gradle
file. Following is the code snippet to declare provided
scope in gradle:
Gradlejava
插件中没有定义 Scope名为provided
. 也不在war
或android
插件中。如果你想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 compile
dependency. compile
dependencies are dependencies, those are necessary to compile your code. Now imagine that if your code uses a library named X
then you must declare X
as your compile-time dependency. Also imagine that X
uses another library Y
internally, and you declared Y
as your runtime dependency.
为了首先回答这个问题,我将定义compile
依赖项。compile
依赖项是依赖项,它们是编译代码所必需的。现在想象一下,如果您的代码使用名为的库,X
那么您必须声明X
为您的编译时依赖项。还可以想象它在内部X
使用另一个库Y
,并且您声明Y
为您的运行时依赖项。
During compilation, Gradle will add X
into your classpath but will not add Y
. Since, Y
is not required for compilation. But it will package both X
and Y
with your project archive since both X
and Y
are necessary to run your project archive in the production environment. Generally, all the dependencies needed in the production environment are known as runtime
dependency.
在编译期间,Gradle 会添加X
到您的类路径中,但不会添加Y
. 因为,Y
不需要编译。但是,这两种包装X
和Y
使用,因为两个项目档案X
,并Y
为需要运行在生产环境中的项目归档。通常,生产环境中所需的所有依赖项都称为runtime
依赖项。
In Gradle official documentation, it says that runtime
dependency 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 provided
is a compile
dependency that we don't want to be present in the runtime
dependency (basically, we don't want it to package with the project archive).
现在,如果您已经读到这里,那么您已经知道这provided
是一个compile
我们不希望出现在runtime
依赖项中的依赖项(基本上,我们不希望它与项目存档一起打包)。
Following is an illustration of provided
and runtime
scope. Here, compile
refers to the dependencies that are required to compile the project and non-compile
refers to the dependencies that are not required for project compilation.
以下是说明provided
和runtime
范围。这里,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。