Platforms and constraints
JUnit publishes a dozen modules that only work together in one combination of versions. The BOM of a release names that combination, and in Gradle the BOM is a platform: a dependency that brings no jar and only constraints. A build that applies it writes the modules with no version at all.
A dependency with no version
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
}
repositories {
mavenLocal()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.14.3')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
src/test/java/com/example/SmokeTest.java
package com.example;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class SmokeTest {
@Test
void runs() {
assertTrue(true);
}
}
Two dependencies have no version, and the build still resolves: the platform(...) line turned the BOM into a set of constraints for the configurations it was declared in. The launcher, which lives in another group (org.junit.platform), gets its version from the same BOM.
What the BOM did
gradle -q dependencyInsight --dependency junit-jupiter-api --configuration testCompileClasspath --console=plain --offline --no-daemon
org.junit.jupiter:junit-jupiter-api:5.14.3 (by constraint)
Variant compile:
| Attribute Name | Provided | Requested |
|--------------------------------|----------|--------------|
| org.gradle.status | release | |
| org.gradle.category | library | library |
| org.gradle.libraryelements | jar | classes |
| org.gradle.usage | java-api | java-api |
| org.gradle.dependency.bundling | | external |
| org.gradle.jvm.environment | | standard-jvm |
| org.gradle.jvm.version | | 21 |
org.junit.jupiter:junit-jupiter-api:5.14.3
+--- org.junit:junit-bom:5.14.3
| \--- testCompileClasspath
+--- org.junit.jupiter:junit-jupiter:5.14.3
| +--- testCompileClasspath (requested org.junit.jupiter:junit-jupiter)
| \--- org.junit:junit-bom:5.14.3 (*)
\--- org.junit.jupiter:junit-jupiter-params:5.14.3
+--- org.junit:junit-bom:5.14.3 (*)
\--- org.junit.jupiter:junit-jupiter:5.14.3 (*)
(*) - 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.
The first line says (by constraint), and the tree shows the path through org.junit:junit-bom:5.14.3. The version was not requested by any dependency: a constraint of the platform supplied it. The same report on the runtime class path shows the launcher the same way:
gradle -q dependencyInsight --dependency junit-platform-launcher --configuration testRuntimeClasspath --console=plain --offline --no-daemon
org.junit.platform:junit-platform-launcher:1.14.3 (by constraint)
Variant runtime:
| Attribute Name | Provided | Requested |
|--------------------------------|--------------|--------------|
| org.gradle.status | release | |
| org.gradle.category | library | library |
| org.gradle.libraryelements | jar | jar |
| org.gradle.usage | java-runtime | java-runtime |
| org.gradle.dependency.bundling | | external |
| org.gradle.jvm.environment | | standard-jvm |
| org.gradle.jvm.version | | 21 |
org.junit.platform:junit-platform-launcher:1.14.3
\--- org.junit:junit-bom:5.14.3
\--- testRuntimeClasspath
org.junit.platform:junit-platform-launcher -> 1.14.3
\--- testRuntimeClasspath
A web-based, searchable dependency report is available by adding the --scan option.
Constraints
A constraint is what a platform is made of, and a build can declare one without a platform: dependencies { constraints { implementation('org.slf4j:slf4j-api:1.7.36') } }. It states a version that applies if the module appears, and adds nothing to the graph by itself. A build that needs a version another module must not lower writes a strictly constraint instead: constraints { implementation('org.slf4j:slf4j-api') { version { strictly '1.7.36' } } }.
A multi-project build can also publish a platform of its own: a project with the java-platform plugin, a constraints block and no sources. The projects that use it write implementation platform(project(':platform')) and share one set of versions.
Related
- Conflict resolution: what happens when two constraints disagree.
- Version catalogs: the version numbers the build script no longer holds.
- dependencyManagement in Maven: the same BOM import in a pom.
Article author: Arthur Isaev