Java Community | Help. Code. Learn.JC|HCL
Java Community | Help. Code. Learn.โ€ข14mo agoโ€ข
6 replies
cire

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
.

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")
Was this page helpful?