Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created November 28, 2020 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordcodes/64147f4c72ede78db43868f8a6120cfa to your computer and use it in GitHub Desktop.
Save lordcodes/64147f4c72ede78db43868f8a6120cfa to your computer and use it in GitHub Desktop.
Brief demo of using a platform module to constrain dependency versions
dependencies {
implementation(enforcedPlatform(project(":dependency-constraints")))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("androidx.lifecycle:lifecycle-livedata-ktx")
implementation("io.coil-kt:coil")
}
plugins {
id("java-platform")
id("maven-publish")
}
javaPlatform {
allowDependencies()
}
dependencies {
api(enforcedPlatform("org.jetbrains.kotlin:kotlin-bom:1.4.20"))
constraints {
api("androidx.lifecycle:lifecycle-livedata-ktx:2.3.0-beta01")
api("io.coil-kt:coil:1.0.0")
}
}
publishing {
publications {
create<MavenPublication>("getBusyPlatform") {
from(components["javaPlatform"])
}
}
}
include("dependency-constraints")
@jraska
Copy link

jraska commented Nov 30, 2020

So this line implementation(enforcedPlatform(project(":dependency-constraints"))) doesn't bring any dependency to classpath whilst compiling? It only brings the constraints for any dependency defined in the same file?

@lordcodes
Copy link
Author

lordcodes commented Nov 30, 2020

It essentially brings in what is in the platform module (depnedency-constraints/build.gradle.kts).
When you apply the java platform plugin by default it disables regular dependency declarations as that is usually a bug and only allows constraints.
However, I also enable dependencies in order to also add the 'kotlin-bom' as a dependency so that by using my platform as enforcedPlatform it will also apply other BOMs as well.
However, yes the dependency declarations inside 'constraints' are simply dependency constraints which mention a version.

If you use platform(...) then it just uses the version as a suggestion, so you can leave it out.
If you use enforcedPlatform(...) then it actually enforces the version and also enforces it for any transitive dependencies.

I might consider an article on this, if I can work out how to word it well.

@jraska
Copy link

jraska commented Nov 30, 2020

Thanks a lot for the insights :)

it disables regular dependency declarations as that is usually a bug

Doesn't this allow it back?

javaPlatform {
    allowDependencies()
}

This or BOMs themselves in general do not bring any jar?
So this: api(enforcedPlatform("org.jetbrains.kotlin:kotlin-bom:1.4.20")) does add zero dependencies? :)

When it comes to an article I think my idea or name would be - "Avoid dependency hell with a power of BOM and Gradle constraints" or something like that :D

@lordcodes
Copy link
Author

Exactly I added 'allowDependencies()' in order to allow the platform to specify real dependencies so that I could specify the BOMs for other frameworks.

Also, yes adding a platform for the kotlin-bom simply brings in constraints on the versions. So that you can simply specify Kotlin dependencies throughout your project without the version, then update the Kotlin version only on the BOM.

Haha amazing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment