Intro
The following situation:
I'm working on multiple dependent Java projects that are built using the Gradle build system.
There are several dependencies that I want to be resolved via the java platform plugin for gradle and a resulting BOM (Bill of Materials).
Example & Problem:
In the example below there is a build.gradle
for the platform BOM that is used as a dependency resolver for all projects. There are two constraints in this BOM: One for an implementation dependency named foo
an another for compile dependency named bar
.
// build.gradle for BOM "my-utility-bom"
// ...
dependencies {
constraints {
api("some.implementation.dependency:foo:3.4.1")
api("some.compile.dependency:bar:1.2.0")
}
}
// ...
Both dependencies are then used inside a project by applying the platform keyword to the given bom project. Because I would like to use the two contraints from the bom as dependencies, I then use implementation
on the platform project and compileOnly
the same way.
// build.gradle for java project
//...
dependencies {
implementation(platform(project(":my-utility-bom")))
compileOnly(platform(project(":my-utility-bom")))
implementation("some.implementation.dependency:foo")
compileOnly("some.compile.dependency:bar")
}
// ...
After that, all dependencies are pulled in as both, a implementation
dependency AND compileOnly
dependency. But I just want the one depenency as an implementation
dependency and the other as a compileOnly
dependency.
Question
Is possible to only pull specific dependencies as implementation
or compileOnly
dependency without the use of separate BOMs for compile and implementation dependencies?
I tried out an example similar to yours and dependencies get resolved as expected. In your case when running
gradle dependencies
:some.implementation.dependency:foo
some.compile.dependency:bar
Then this will be resolved in
compileClasspath
importing both resolved dependenciessome.implementation.dependency:foo:3.4.1
andsome.compile.dependency:bar:1.2.0
runtimeClasspath
will import onlysome.implementation.dependency:foo:3.4.1
I would say that your configuration is correct. In case your dependency tree say otherwise, then maybe there are dependencies applied somehow additionally not visible from your example. Would be helpful to see your actual case. Note I used Gradle 7.0. In this repository there is whole example.
This graph helped me understand how different configuration will be resolved