16

Now I use this way:

plugins {
    val kotlinVersion: String by project
    val springBootPluginVersion: String by project
    val springDependencyManagementPluginVersion: String by project

    id("org.jetbrains.kotlin.plugin.allopen") version kotlinVersion
    id("org.jetbrains.kotlin.jvm") version kotlinVersion
    id("org.springframework.boot") version springBootPluginVersion
    id("io.spring.dependency-management") version springDependencyManagementPluginVersion
}

This variant compiles and works, but I don't know is this way right and why IntelliJ IDEA shows error on lines where placed versions definitions:

'val Build_gradle.project: Project' can't be called in this context by implicit receiver. Use the explicit one if necessary
msrd0
  • 7,816
  • 9
  • 47
  • 82
Roman Q
  • 245
  • 3
  • 16

2 Answers2

9

(Cross-post: source)

Apparently this has become possible recently, if it wasn't possible in the past. (Almost) from the docs:

gradle.properties:

helloPluginVersion=1.0.0

settings.gradle.kts:

pluginManagement {
  val helloPluginVersion: String by settings
  plugins {
    id("com.example.hello") version helloPluginVersion
  }
}

And now the docs say that build.gradle.kts should be empty but my testing shows that you still need this in build.gradle.kts:

plugins {
  id("com.example.hello")
}

The version is now determined by settings.gradle.kts and hence by gradle.properties which is what we want...

Peter V. Mørch
  • 13,830
  • 8
  • 69
  • 103
4

There are a couple issues that have some details around this:

The way to do this in the most recent verions of Gradle is to use settings.gradle or settings.gradle.kts and the pluginManagement {} block.

In your case, it could look like:

pluginManagement {
  resolutionStrategy {
    eachPlugin {
      when (requested.id.id) {
        "org.jetbrains.kotlin.plugin.allopen" -> {
          val kotlinVersion: String by settings
          useVersion(kotlinVersion)
        }
        "org.jetbrains.kotlin.jvm" -> {
          val kotlinVersion: String by settings
          useVersion(kotlinVersion)
        }
        "org.springframework.boot" -> {
          val springBootPluginVersion: String by settings
          useVersion(springBootPluginVersion)
        }
        "io.spring.dependency-management" -> {
          val springDependencyManagementPluginVersion: String by settings
          useVersion(springDependencyManagementPluginVersion)
        }
      }
    }
  }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 2
    Thank you. This code seems verbose, but probably this is better than define variable for each plugin in each module. – Roman Q Oct 16 '18 at 04:51
  • I have used this approach with some modifications, see https://github.com/rkudryashov/microservices-example – Roman Q Jan 26 '19 at 11:03