Error: plugin already on the classpath must not include a version

I didn’t find any reference to this use case in Grade plugins documentation.

I also had an issue where a Gradle project could be built as standalone or as a sub-project of another Gradle project and I wanted to apply sonarqube in the sub-project as well as the root project.

Currently, I can think of 2 “workarounds”:

  1. In your sub project, instead of applying with the plugins{} block, apply the plugin conditionally using legacy plugin application. This is the method I used:
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
  }
}

def kotlinPluginId = 'org.jetbrains.kotlin.multiplatform'
final hasPlugin = project.getPlugins().hasPlugin(kotlinPluginId);
if (hasPlugin) {
    final Plugin plugin = project.getPlugins().getPlugin(kotlinPluginId)
    println 'Plugin already applied - version ' + plugin.properties['kotlinPluginVersion']
} else {
    apply plugin: "org.jetbrains.kotlin.multiplatform"
}
  1. Create a dummy root project for the standalone project and use plugins{} block there for the sub-project. In this case the sub-project won’t have any logic to apply the plugin.

I hope this helps or gives an idea how to approach this issue.

1 Like