A major difference between a Maven BOM and a Java platform is that in Gradle dependencies and
constraints
are declared and scoped to a configuration and the ones extending it.
While many users will only care about declaring constraints for
compile time
dependencies, thus inherited by runtime and tests ones, it allows declaring dependencies or constraints that only apply to runtime or test.
For this purpose, the plugin exposes two
configurations
that can be used to declare dependencies:
api
and
runtime
.
The
api
configuration should be used to declare constraints and dependencies which should be used when compiling against the platform, whereas the
runtime
configuration should be used to declare constraints or dependencies which are visible at runtime.
build.gradle.kts
dependencies {
constraints {
api("commons-httpclient:commons-httpclient:3.1")
runtime("org.postgresql:postgresql:42.2.5")
}
}
build.gradle
dependencies {
constraints {
api 'commons-httpclient:commons-httpclient:3.1'
runtime 'org.postgresql:postgresql:42.2.5'
}
}
Note that this example makes use of
constraints
and not dependencies.
In general, this is what you would like to do: constraints will only apply if such a component is added to the dependency graph, either directly or transitively.
This means that all constraints listed in a platform would not add a dependency unless another component brings it in: they can be seen as
recommendations
.
|
For example, if a platform declares a constraint on
org:foo:1.1
, and that nothing else brings in a dependency on
foo
,
foo
will
not
appear in the graph.
However, if
foo
appears, then usual conflict resolution would kick in.
If a dependency brings in
org:foo:1.0
, then we would select
org:foo:1.1
to satisfy the platform constraint.
|
By default, in order to avoid the common mistake of adding a dependency in a platform instead of a constraint, Gradle will fail if you try to do so.
If, for some reason, you also want to add
dependencies
in addition to constraints, you need to enable it explicitly:
build.gradle.kts
javaPlatform {
allowDependencies()
}
build.gradle
javaPlatform {
allowDependencies()
}