Gradle的配置层次结构如何工作? [英] How does Gradle's configurations hierarchy work?

查看:88
本文介绍了Gradle的配置层次结构如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有四种基本配置,编译,运行时,testCompile和testRuntime。如果我放入这样的依赖项:

 运行组:'org.apache.logging.log4j',名称:'log4j -core',版本: 2.3 

这意味着此依赖项在运行时编译,对吗?但是,testCompile和testRuntime呢?这些配置也可以使用吗?如果添加自己的配置,是否必须指定它在层次结构中的位置?如果我不这样做会怎样?文档并没有真正弄清楚。

解决方案

这4种配置的定义如下java插件:



编译
编译项目生产源所需的依赖项。



运行时
生产类在运行时所需的依赖项。默认情况下,还包括编译时间相关性。



testCompile
编译项目测试源所需的相关性。默认情况下,还包括编译后的生产类和编译时依赖项。



testRuntime
运行测试所需的依赖项。默认情况下,还包括编译,运行时和测试编译依赖项。



您还可以检查



在声明新配置时,您可以定义什么它扩展了其他配置,例如 Gradle In Action Geb 为例,您将新配置定义为

 配置{
functTestCompile.extendsFrom testCompile
functTestRuntime.extendsFrom testRuntime
}

如果您不这样做,则认为这些配置不需要从另一个配置及其配置中受益串联时,您将需要定义此配置所需的所有依赖项。


I know there are four basic configurations, compile, runtime, testCompile, and testRuntime. If I put in a dependency like this:

runtime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.3'

This means this dependency is available under runtime and compile, correct? But what about testCompile and testRuntime? Is it available for these configurations as well? If I add my own configuration, do I have to specify where it exists in the hierarchy? What happens if I don't? The documentation didn't really make this clear.

解决方案

The definition for those 4 configuration are as follow for the java plugin :

compile The dependencies required to compile the production source of the project.

runtime The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.

testCompile The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.

testRuntime The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.

you can also check https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations, it has pretty graph and table:

When you declare a new configuration you can define what other configuration it extends, for example Gradle In Action takes the example with Geb, you would define new configuration as

configurations {
    functTestCompile.extendsFrom testCompile
    functTestRuntime.extendsFrom testRuntime
}

If you dont, you assume those configuration do not need to benefit from another one and its standalone, you will need to define all dependencies this configuration requires.

这篇关于Gradle的配置层次结构如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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