How can I dynamically change a plugin's version depending on certain criteria?

We have a multi platform project, however each platform has its own platform-dependent implementation of some classes. In our case, 2 of these target platforms require the same plugin, however they rely on different versions. So far, I have tried to dynamically set extra[pluginVersion] via either -PbuildingForB and gradle.taskGraph.whenReady , then setting a classpath in the dependencies block of the buildscript block, and finally applying the plugin after the plugins block. Unfortunately, this errors with Cannot get property 'pluginVersion' on extra properties extension as it does not exist.
gradle.build.kts
buildscript {
extra {
"pluginVersionA" to "1.0"
"pluginVersionB" to "2.0"
"pluginVersion" to "1.0"
}

extra["pluginVersion"] = project.findProperty("buildingForB")?.let {
if (it == "true") extra["pluginVersionB"] else extra["pluginVersionA"]
} ?: extra["pluginVersionA"]

gradle.taskGraph.whenReady {
if (tasks.contains(task("buildPlatformB")))
extra["pluginVersion"] = extra["pluginVersionB"];
}

repositories {
mavenCentral()
}
dependencies {
classpath("com.example:example-plugin:${extra["pluginVersion"]}")
}
}

plugins {
...
}

apply(plugin = "org.example")
gradle.build.kts
buildscript {
extra {
"pluginVersionA" to "1.0"
"pluginVersionB" to "2.0"
"pluginVersion" to "1.0"
}

extra["pluginVersion"] = project.findProperty("buildingForB")?.let {
if (it == "true") extra["pluginVersionB"] else extra["pluginVersionA"]
} ?: extra["pluginVersionA"]

gradle.taskGraph.whenReady {
if (tasks.contains(task("buildPlatformB")))
extra["pluginVersion"] = extra["pluginVersionB"];
}

repositories {
mavenCentral()
}
dependencies {
classpath("com.example:example-plugin:${extra["pluginVersion"]}")
}
}

plugins {
...
}

apply(plugin = "org.example")
4 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @cire! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
Why are you using extra as opposed to just defining a variable?
cire
cireOP2mo ago
Well I tried using a variable, the same issue occurred and I thought it might be some weird scoping issue, so i tried extra using this
val versionA = "1.0"
val versionB = "2.0"
var pluginVersion = versionA

pluginVersion = project.findProperty("buildingForB")?.let {
if (it == "true") versionB else versionA
} ?: versionA

gradle.taskGraph.whenReady {
if (tasks.contains(task("buildPlatformB")))
pluginVersion = versionB
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.example:example-plugin:${pluginVersion]}")
}
}

apply(plugin = "com.example")
val versionA = "1.0"
val versionB = "2.0"
var pluginVersion = versionA

pluginVersion = project.findProperty("buildingForB")?.let {
if (it == "true") versionB else versionA
} ?: versionA

gradle.taskGraph.whenReady {
if (tasks.contains(task("buildPlatformB")))
pluginVersion = versionB
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.example:example-plugin:${pluginVersion]}")
}
}

apply(plugin = "com.example")
errors with Unresolved reference: pluginVersion
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?