java, application & java-library
Gradle's core knows nothing about Java. Three core plugins teach it, each building on the previous one:
| Plugin | Adds | Use it for |
|---|---|---|
java |
source sets main and test, tasks compileJava, test, jar, javadoc, the configurations implementation, compileOnly, runtimeOnly, testImplementation, … |
any Java project |
java-library |
java + the api configuration (and compileOnlyApi) |
code that other projects compile against |
application |
java + run, startScripts, installDist, distZip/distTar |
a program with a main method |
A typical multi-project build applies java-library to its libraries and application to the one project that starts the program.
api vs implementation
This is the most important distinction in Gradle's Java support, and Maven has nothing like it. A library's dependencies come in two kinds:
implementation: used inside the library. Consumers get it on their runtime classpath (the library needs it to run), but not on their compile classpath.api: part of the library's public API (a type in a public method signature, a superclass). Consumers get it on both classpaths.
Maven's compile scope behaves like api for everything, so every transitive dependency leaks onto every consumer's compile classpath. With implementation, a consumer can't accidentally start using your internals, and changing an implementation dependency doesn't force consumers to recompile.
In the session, lib returns a Guava ImmutableList from a public method but declares Guava as implementation. app uses the returned type:
mkdir -p shop/lib/src/main/java/shop/lib shop/app/src/main/java/shop/app && cd shop
cat > settings.gradle.kts <<'KTS'
rootProject.name = "shop"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("lib", "app")
KTS
cat > lib/build.gradle.kts <<'KTS'
plugins { `java-library` }
dependencies {
implementation("com.google.guava:guava:33.7.2-jre")
}
KTS
cat > lib/src/main/java/shop/lib/Catalog.java <<'SRC'
package shop.lib;
import com.google.common.collect.ImmutableList;
public class Catalog {
public static ImmutableList<String> products() {
return ImmutableList.of("coffee", "bagel");
}
}
SRC
cat > app/build.gradle.kts <<'KTS'
plugins { application }
dependencies { implementation(project(":lib")) }
application { mainClass = "shop.app.Main" }
KTS
cat > app/src/main/java/shop/app/Main.java <<'SRC'
package shop.app;
import com.google.common.collect.ImmutableList;
import shop.lib.Catalog;
public class Main {
public static void main(String[] args) {
ImmutableList<String> products = Catalog.products();
System.out.println(products.reverse());
}
}
SRC
echo "--- guava as implementation"
gradle -q :app:compileJava 2>&1 | sed -n '/^\* What went wrong/,/^\* Try/p' | head -6
gradle -q :app:dependencies --configuration compileClasspath | grep -- '--- '
echo "--- guava as api"
sed -i 's/implementation("com.google.guava/api("com.google.guava/' lib/build.gradle.kts
gradle -q :app:run
gradle -q :app:dependencies --configuration compileClasspath | grep -- '--- '--- guava as implementation
* What went wrong:
Execution failed for task ':app:compileJava' (registered by plugin class 'org.gradle.api.plugins.JavaBasePlugin').
> Compilation failed; see the compiler output below.
/home/learner/shop/app/src/main/java/shop/app/Main.java:3: error: package com.google.common.collect does not exist
import com.google.common.collect.ImmutableList;
^
\--- project ':lib'
--- guava as api
[bagel, coffee]
\--- project ':lib'
\--- com.google.guava:guava:33.7.2-jre
+--- com.google.guava:failureaccess:1.0.3
+--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
+--- org.jspecify:jspecify:1.0.1
+--- com.google.errorprone:error_prone_annotations:2.50.0
\--- com.google.j2objc:j2objc-annotations:3.1
With implementation, app's compile classpath contained only project :lib: javac saw Catalog.products() return a type it couldn't load. Declaring Guava as api fixed it, because ImmutableList is part of lib's API. If lib returned a plain List<String> instead, implementation would be the right choice, and app would have to declare Guava itself to use it directly.
The rule of thumb: start with implementation; switch to api only for types that appear in your public signatures.
The application plugin
application turns a project into a runnable program: run starts it, and installDist builds a self-contained folder with every runtime JAR in lib/ and start scripts in bin/:
mkdir -p tool/src/main/java/demo && cd tool
cat > settings.gradle.kts <<'KTS'
rootProject.name = "tool"
KTS
cat > build.gradle.kts <<'KTS'
plugins { application }
repositories { mavenCentral() }
dependencies {
implementation("org.apache.commons:commons-lang3:3.21.0")
runtimeOnly("org.slf4j:slf4j-simple:2.0.20")
compileOnly("org.jspecify:jspecify:1.0.0")
}
version = "1.2.0"
application {
mainClass = "demo.Tool"
applicationDefaultJvmArgs = listOf("-Dtool.mode=cli")
}
KTS
cat > src/main/java/demo/Tool.java <<'SRC'
package demo;
import org.apache.commons.lang3.StringUtils;
import org.jspecify.annotations.Nullable;
public class Tool {
static String label(@Nullable String s) { return s == null ? "?" : StringUtils.capitalize(s); }
public static void main(String[] args) {
System.out.println(label(args.length > 0 ? args[0] : null) + " mode=" + System.getProperty("tool.mode"));
}
}
SRC
gradle -q run --args="hello"
gradle -q installDist
find build/install -type f | sort
build/install/tool/bin/tool installedHello mode=cli
build/install/tool/bin/tool
build/install/tool/bin/tool.bat
build/install/tool/lib/commons-lang3-3.21.0.jar
build/install/tool/lib/slf4j-api-2.0.20.jar
build/install/tool/lib/slf4j-simple-2.0.20.jar
build/install/tool/lib/tool-1.2.0.jar
Installed mode=cli
The distribution contains commons-lang3 (implementation) and slf4j-simple (runtimeOnly), but not jspecify: compileOnly is Gradle's equivalent of Maven's provided. The bin/tool script sets the classpath and the JVM arguments from applicationDefaultJvmArgs; distZip packs the same folder into a ZIP for release.
Source sets
A source set is a group of sources with its own classpaths and compile task. java creates main and test; you can add more, for example for integration tests:
sourceSets {
create("integrationTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}
}Gradle then adds compileIntegrationTestJava and the configurations integrationTestImplementation and integrationTestRuntimeOnly. (For tests specifically, the JVM Test Suite plugin's testing { suites { … } } block does this wiring for you.)
Test yourself
A library's public method returns a Guava ImmutableList. How should the library declare Guava?
- implementation
- api
- compileOnly
- runtimeOnly
What does a consumer of a java-library get from the library's implementation dependencies?
- Nothing
- Them on its runtime classpath only
- Them on its compile classpath only
- Them on both classpaths
Which Gradle configuration corresponds to Maven's provided scope?
- runtimeOnly
- compileOnly
- api
- testImplementation