How does Gradle's configurations hierarchy wor

2019-07-10 03:10发布

问题:

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.

回答1:

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.