Configurations
A dependency is not just "a jar": it is a jar on one of several class paths. The configuration you put it in decides whether the code can see it while compiling, whether the program sees it at run time, and whether a project that depends on yours sees it at all. This lesson puts one dependency in each configuration and prints the two class paths that come out.
A dependency in each configuration
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java-library'
}
repositories {
mavenLocal()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.14.0'
compileOnly 'org.slf4j:slf4j-api:1.7.36'
runtimeOnly 'org.apache.commons:commons-text:1.12.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.14.3'
}
src/main/java/com/example/Text.java
package com.example;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
public final class Text {
public static String reverse(String text, Logger log) {
log.debug("reversing {}", text);
return StringUtils.reverse(text);
}
}
Text uses StringUtils of commons-lang3 and Logger of slf4j-api, and both of them are on the compile class path. The file also compiles, because both jars are visible to javac.
compileClasspath
gradle -q dependencies --configuration compileClasspath --console=plain --offline --no-daemon
------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------
compileClasspath - Compile classpath for source set 'main'.
+--- org.slf4j:slf4j-api:1.7.36
\--- org.apache.commons:commons-lang3:3.14.0
A web-based, searchable dependency report is available by adding the --scan option.
Two direct dependencies: implementation and compileOnly. The test dependency is not here, because the main code is not compiled against it.
runtimeClasspath
gradle -q dependencies --configuration runtimeClasspath --console=plain --offline --no-daemon
------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------
runtimeClasspath - Runtime classpath of source set 'main'.
+--- org.apache.commons:commons-lang3:3.14.0
\--- org.apache.commons:commons-text:1.12.0
\--- org.apache.commons:commons-lang3:3.14.0
(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.
A web-based, searchable dependency report is available by adding the --scan option.
Also two, and only one of them is the same: implementation stays, compileOnly is gone and runtimeOnly took its place. A program that calls Logger would compile and then fail with NoClassDefFoundError, because the interface it was compiled against is not on the class path at run time; that is exactly what compileOnly says.
The configurations
| configuration | on the compile class path | at run time | in a project that depends on this one |
|---|---|---|---|
| implementation | yes | yes | only at run time, invisible to its compiler |
| api (with java-library) | yes | yes | yes, on its compile class path too |
| compileOnly | yes | no | no |
| runtimeOnly | no | yes | only at run time |
| testImplementation | only for the test source set | only for a test run | no |
api exists so that a library can say which of its dependencies appear in its own signatures. Everything else belongs in implementation, which keeps the compile class path of the projects that use the library short.
Related
- gradle dependencies and dependencyInsight: reading the two lists of this lesson in full.
- Platforms and constraints: one version for a family of modules.
- Dependencies in Maven: the same choice, written as a scope.
Автор статьи: Arthur Isaev