Sub topic 11
Dependency Configurations in Gradle
In Maven, you give each dependency a scope. Gradle does the same job with configurations. A configuration is a named group of dependencies, and its name tells Gradle when the library is needed. Some libraries are needed while compiling, some only while the program runs, and some only while tests run.
Choosing the right configuration keeps your build small. Test libraries stay out of the finished app, and code only uses the libraries it should.
The Configurations You Will Use Most
Each line in the dependencies { } block starts with a configuration name. The text in quotes is the library's coordinates in the form group:artifact:version.
| Configuration | Needed to compile? | Needed to run? | Closest Maven scope |
|---|---|---|---|
| `implementation` | Yes | Yes | `compile` |
| `compileOnly` | Yes | No | `provided` |
| `runtimeOnly` | No | Yes | `runtime` |
| `testImplementation` | Yes, for tests | Yes, for tests | `test` |
| `testRuntimeOnly` | No | Yes, for tests | `test` |
Gradle turns these configurations into classpaths. A classpath is the list of places where Java looks for classes. For example, the compileClasspath is the list javac uses, and the runtimeClasspath is the list used when the app runs.
Test classpaths also include everything from implementation. Your tests can use the same libraries as your main code.
A Worked Example
Here is the dependencies block from this tutorial's build.gradle.kts:
dependencies {
implementation("com.google.guava:guava:33.2.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}- Guava is used by the app's own code, so it goes in
implementation. - JUnit Jupiter gives you
@TestandassertEquals. Test code calls these directly, so it goes intestImplementation. - The JUnit Platform Launcher starts the tests, but your code never calls it. It is only needed at test run time, so it goes in
testRuntimeOnly. It has no version number because JUnit Jupiter tells Gradle which launcher version matches it.
To see what ended up on a classpath, ask Gradle for a report:
./gradlew dependencies --configuration runtimeClasspath
./gradlew dependencies --configuration testRuntimeClasspathGuava shows up in both reports. JUnit shows up only in the test report.
Exercise
Your app uses a database driver that the code never imports by name; it is loaded only while the program runs. Which Gradle configuration should it go in, and what is the closest Maven scope?
Show answer
Use runtimeOnly, because the driver is needed on the runtime classpath but not for compiling. The closest Maven scope is runtime.
Exercise
A teammate puts junit-jupiter in implementation instead of testImplementation. The tests still pass. What is wrong with this?
Show answer
JUnit is now on the main compile and runtime classpaths, so it ships with the app and main code could import it by mistake. It belongs in testImplementation so that only tests can see it.