Sub topic 11
Dependency Configurations in Gradle
In Maven, you give each dependency a scope, such as compile or test. Gradle does the same job with configurations. A configuration is a named bucket of dependencies. Each bucket has a set of rules about when its contents are available: while compiling, while running, or only during tests.
This chapter covers the common Gradle configurations, how they map onto Maven scopes, and how to check which configuration a library ended up in.
Why configurations matter
A Java program uses libraries at two different times:
- Compile time is when
javacturns your source code into class files. The compiler needs to see every class your code mentions. - Runtime is when the
javacommand runs the program. The JVM needs every class that actually gets loaded.
These two lists are often different. Some libraries are needed only at runtime, such as a database driver that is loaded by name. Some are needed only at compile time, such as an annotation-only library. Test libraries like JUnit should never ship with your application at all.
A classpath is the list of JAR files and folders where Java looks for classes. Gradle builds a separate classpath for each of these moments. The configurations you write in your build script decide which libraries go onto which classpath.
The configurations you will use most
The java plugin creates these configurations. The application plugin includes the java plugin, so you get them there too.
| Configuration | Compile main code | Run main code | Compile tests | Run tests | Closest Maven scope |
|---|---|---|---|---|---|
| `implementation` | yes | yes | yes | yes | `compile` |
| `compileOnly` | yes | no | no | no | `provided` |
| `runtimeOnly` | no | yes | no | yes | `runtime` |
| `testImplementation` | no | no | yes | yes | `test` |
| `testCompileOnly` | no | no | yes | no | (none) |
| `testRuntimeOnly` | no | no | no | yes | `test` |
Read each row as a question: "If I put a library here, where can I use it?"
implementationis the default choice. Use it for any library your main code calls directly.compileOnlyis for libraries that something else provides at runtime. A web server that already includes the Servlet API is a typical example.runtimeOnlyis for libraries your code never names directly but needs while running. JDBC drivers and logging backends are common examples.testImplementationis for libraries your test code calls, such as JUnit's assertions.testRuntimeOnlyis for libraries tests need only while running. The JUnit Platform launcher goes here.
The four boxes on the right are resolvable configurations. Gradle fills them in from the buckets on the left, then hands them to the compiler and the JVM. You never add dependencies to the right-hand boxes yourself. You only read them.
A worked example
Here is a complete build.gradle.kts that uses several configurations:
plugins {
application
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.guava:guava:33.2.1-jre")
runtimeOnly("org.slf4j:slf4j-simple:2.0.13")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
application {
mainClass = "com.example.App"
}
tasks.named<Test>("test") {
useJUnitPlatform()
}Each dependency is written as one string: "group:name:version". These are the same artifact coordinates Maven uses, joined by colons.
The main code can now call Guava, because Guava is in implementation:
package com.example;
import com.google.common.base.Strings;
public class App {
public static void main(String[] args) {
System.out.println(greeting("World"));
System.out.println(Strings.repeat("=", 13));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}The slf4j-simple logger is in runtimeOnly. If you tried to import org.slf4j.simple.SimpleLogger in App.java, compilation would fail, because that library is not on the compile classpath. This is the point of the setup. It stops your code from depending on something by accident.
Checking where a dependency landed
Use the dependencies task to print what Gradle resolved for one classpath:
./gradlew dependencies --configuration compileClasspath
./gradlew dependencies --configuration runtimeClasspath
./gradlew dependencies --configuration testRuntimeClasspathFor the build above, compileClasspath lists Guava but not slf4j-simple. runtimeClasspath lists both. testRuntimeClasspath lists all of them plus JUnit. Nested lines in the output are transitive dependencies, meaning libraries pulled in by your libraries. A later chapter covers those.
To ask why one particular library is present, use dependencyInsight:
./gradlew dependencyInsight --dependency guava --configuration runtimeClasspath`api` versus `implementation` in libraries
If your project is a library that other projects use, apply the java-library plugin instead of application. It adds one more configuration, api.
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
api("com.google.guava:guava:33.2.1-jre")
implementation("org.apache.commons:commons-lang3:3.14.0")
}The difference only shows up for projects that use your library:
apidependencies leak to consumers' compile classpath. Useapiwhen a type from that library appears in your public methods, such as a method that returns a GuavaImmutableList.implementationdependencies stay hidden from consumers at compile time. They still reach consumers' runtime classpath, so the code still works.
Keeping dependencies as implementation means consumers recompile less often and cannot accidentally use your internal libraries. Start with implementation and switch to api only when a type appears in your public API.
Edge cases and common mistakes
- Old tutorials use
compileandtestCompile. These were removed in Gradle 7. Replacecompilewithimplementation(orapi) andtestCompilewithtestImplementation. - Annotation processors need their own configuration. Tools like Lombok generate code during compilation. They go in
annotationProcessor, usually together withcompileOnly:
dependencies {
compileOnly("org.projectlombok:lombok:1.18.32")
annotationProcessor("org.projectlombok:lombok:1.18.32")
}compileOnlylibraries are not available to tests. If your tests need one, also add it totestCompileOnlyortestImplementation.- "Works in my IDE, fails at runtime" usually means a library is in
compileOnlybut nothing provides it at runtime. CheckruntimeClasspathwith thedependenciestask. - "Package does not exist" during compilation usually means the library is in
runtimeOnlyortestImplementationbut your main code imports it.
Maven scopes side by side
| Maven scope | Gradle equivalent |
|---|---|
| `compile` | `implementation`, or `api` in a library |
| `provided` | `compileOnly` |
| `runtime` | `runtimeOnly` |
| `test` | `testImplementation` and `testRuntimeOnly` |
Gradle splits some Maven scopes into finer pieces. Maven's compile scope always leaks to consumers. Gradle lets you choose between api and implementation.
Exercise
Your application connects to PostgreSQL through plain JDBC. Your code only uses java.sql classes and loads the driver by its URL. Which configuration should the PostgreSQL driver use, and how do you confirm it is not on the compile classpath?
Show answer
Use runtimeOnly("org.postgresql:postgresql:<version>"), because your code never imports driver classes but the JVM needs them at runtime. Run ./gradlew dependencies --configuration compileClasspath and check that postgresql is absent, then run it with --configuration runtimeClasspath and check that it is present.
Exercise
You are writing a library with the java-library plugin. One public method returns com.google.common.collect.ImmutableList, and internally you use commons-lang3 for string helpers. Which configuration should each library use?
Show answer
Guava goes in api, because its ImmutableList type is part of your public method signature and consumers must compile against it. commons-lang3 goes in implementation, because it is used only internally and consumers should not see it on their compile classpath.
Exercise
A build script copied from an old blog post contains testCompile("junit:junit:4.13.2") and fails on Gradle 8. What is wrong, and what is the fix?
Show answer
The testCompile configuration was removed in Gradle 7. Change it to testImplementation("junit:junit:4.13.2").