I use the following to keep version strings in a single file (gradle.properties) and use them from either the build script or settings.gradle.kts
val kotlinVersion: String by settings
pluginManagement {
resolutionStrategy {
eachPlugin {
when(requested.id.id) {
"org.jetbrains.kotlin.jvm" -> useVersion(kotlinVersion)
However, this doesn’t work with gradle 6. I’m getting
Unresolved reference: kotlinVersion
when trying to run a build.
Is there a way I can still achieve the goal of keeping all version strings in gradle.properties and access them in the settings file?
Thanks.
1 Like
roip890
(Roi Peretz)
May 14, 2020, 3:36pm
2
I have the same problem when migrate from 5.6.4 to 6.4.
did you found what is the problem?
No, we are still using 5.6
Forinil
(Konrad Botor)
May 21, 2020, 12:03pm
4
I had the same problem in Gradle 6.4.1. This is the solution I found:
pluginManagement {
fun PluginManagementSpec.loadProperties(fileNameWithoutExtension: String, path: String = rootDir.absolutePath): java.util.Properties {
val properties = java.util.Properties()
File("$path/$fileNameWithoutExtension.properties").inputStream().use {
properties.load(it)
}
return properties
}
val pluginVersions: java.util.Properties = loadProperties("pluginVersions")
val dockerRunPluginVersion: String by pluginVersions
resolutionStrategy {
eachPlugin {
when (requested.id.id) {
"com.palantir.docker-run" -> useVersion(dockerRunPluginVersion)
}
}
}
}
Essentially it look like the PluginManagementSpec
object does not see any imports, variables or extension functions declared outside itself.
EDIT: Obviously, I have plugin versions stored in pluginVersions.properties
rather than gradle.properties
, but the underlying principle is the same.
1 Like
Thanks for this, fits perfect.
I kotlinyfied it slightly as I couldn’t resist
fun PluginManagementSpec.loadProperties(fileName: String, path: String = rootDir.absolutePath) = java.util.Properties().also { properties ->
File("$path/$fileName").inputStream().use {
properties.load(it)
}
}
val versions: java.util.Properties = loadProperties("gradle.properties")
renclav
(Dylan Green)
August 27, 2020, 3:09pm
6
for those still looking, the
val kotlinVersion: String by settings
declaration needs to be moved into the plugin management block.
See here for details
imany
(Iman)
January 22, 2021, 9:33am
7
how about loading id
and version
inside plugins{}
block using a constant String declared in a kotlin object.
I am trying that and couldn’t resolve Kotlin objects references.