简体   繁体   English

Gradle 中的“提供”依赖项

[英]'Provided' dependency in Gradle

I have build.gradle in front of me and there are some dependencies declared as provided but in documentation I 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 中providedruntime依赖范围有什么区别?

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.假设需要一个jar来编译您的代码,但该 jar 存在于生产环境库集合中。 Then you don't need to package the jar with your project archives.然后您不需要将 jar 与您的项目档案一起打包。 To support this requirement, Maven has a scope named provided .为了支持这个需求,Maven 有一个名为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.如果您将任何 jar 依赖项声明为provided ,则该 jar 将在编译期间出现在您的类路径中,但不会与您的项目存档一起打包。

provided scope is very useful, particularly in web applications. provided范围非常有用,尤其是在 Web 应用程序中。 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 .例如, servlet-api.jar需要出现在您的类路径中以编译您的项目,但您不需要它来将servlet-api.jar文件与您的war一起打包。 With provided scope one can achieve this requirement.通过provided范围,可以实现这一要求。

There is no Scope defined in Gradle java plugin named provided . Gradle java插件中没有定义 Scope 命名为provided Also not in war or android plugins.也不在warandroid插件中。 If you want to use provided scope in your project, then you have to define it in your build.gradle file.如果你想在你的项目中使用provided范围,那么你必须在你的build.gradle文件中定义它。 Following is the code snippet to declare provided scope in gradle:以下是在 gradle 中声明provided范围的代码片段:

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依赖项。 compile dependencies are dependencies, those are necessary to compile your code. compile依赖项是依赖项,它们是编译代码所必需的。 Now imagine that if your code uses a library named X then you must declare X as your compile-time dependency.现在想象一下,如果你的代码使用库命名为X ,那么你必须声明X为你的编译时依赖。 Also imagine that X uses another library Y internally, and you declared Y as your runtime dependency.还可以想象X内部使用另一个库Y ,并且您将Y声明为您的运行时依赖项。

During compilation, Gradle will add X into your classpath but will not add Y .在编译期间,Gradle 会将X添加到您的类路径中,但不会添加Y Since, Y is not required for compilation.因为,编译不需要Y 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.但它会将XY与您的项目存档一起打包,因为XY都是在生产环境中运行您的项目存档所必需的。 Generally, all the dependencies needed in the production environment are known as runtime dependency.通常,生产环境中所需的所有依赖项都称为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.以下是providedruntime范围的说明。 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.这里的compile指的是编译工程需要的依赖, non-compile指的是工程编译不需要的依赖。

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

For further clarification, as of the latest version, Gradle 5.5 has compileOnly (same as provided ) and runtimeOnly options.为了进一步说明,从最新版本开始,Gradle 5.5 具有compileOnly (与provided相同)和runtimeOnly选项。 The new default "compile and runtime" option is implementation .新的默认“编译和运行时”选项是implementation

Updating the answer as per the latest gradle versions.根据最新的 gradle 版本更新答案。

From gradle's official documentation at below link:来自以下链接的 gradle 官方文档:

https://docs.gradle.org/current/userguide/upgrading_version_5.html https://docs.gradle.org/current/userguide/upgrading_version_5.html

Deprecations弃用

Dependencies should no longer be declared using the compile and runtime configurations.不应再使用编译和运行时配置声明依赖项。 The usage of the compile and runtime configurations in the Java ecosystem plugins has been discouraged since Gradle 3.4.从 Gradle 3.4 开始,不鼓励在 Java 生态系统插件中使用编译和运行时配置。

The implementation, api, compileOnly and runtimeOnly configurations should be used to declare dependencies and the compileClasspath and runtimeClasspath configurations to resolve dependencies.应该使用 implementation、api、compileOnly 和 runtimeOnly 配置来声明依赖项,并使用 compileClasspath 和 runtimeClasspath 配置来解析依赖项。

More so, the compile dependency configuration has been removed in the recently released Gradle 7.0 version.更重要的是,最近发布的 Gradle 7.0 版本中删除了编译依赖项配置。

If you try to use compile in your Gradle 3.4+ project you'll get a warning like this:如果您尝试在 Gradle 3.4+ 项目中使用 compile,您将收到如下警告:

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容。 Use '–warning-mode all' to show the individual deprecation warnings.使用“--warning-mode all”来显示各个弃用警告。

You should always use implementation rather than compile for dependencies, and use runtimeOnly instead of runtime.您应该始终使用实现而不是编译依赖项,并使用 runtimeOnly 而不是运行时。

War plugin战争插件

The War plugin extends the Java plugin to add support for assembling web application WAR files. War 插件扩展了 Java 插件以添加对组装 Web 应用程序 WAR 文件的支持。 It disables the default JAR archive generation of the Java plugin and adds a default WAR archive task.它禁用 Java 插件的默认 JAR 存档生成并添加默认 WAR 存档任务。

The War plugin adds two dependency configurations: War插件增加了两个依赖配置:

  1. providedCompile提供编译
  2. providedRuntime提供运行时

Adding an entry to providedCompile or providedRuntime will cause that dependency to be excluded from the war file.向providedCompile 或providedRuntime 添加条目将导致从war 文件中排除该依赖项。

  1. Use providedCompile if you have source that relies on some classes for compiling.如果您的源代码依赖于某些类进行编译,请使用 providedCompile。
  2. Use providedRuntime if you use it for testing and not compiling.如果将providedRuntime 用于测试而不是编译,请使用它。

Example:例子:

providedCompile 'org.springframework.boot:spring-boot-starter-tomcat:1.1.6.RELEASE'

The above JAR and its transitive dependency will only be available at compile time but it will not be available at runtime.上述 JAR 及其传递依赖项仅在编译时可用,但在运行时不可用。 It means, those JAR will not be included in war archive.这意味着,那些 JAR 将不会包含在战争档案中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM