如何在Gradle Kotlin DSL中使用gradle.properties中的插件版本?

时间:2018-10-14 07:41:42

标签: gradle intellij-idea kotlin gradle-kotlin-dsl

现在我用这种方式:

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
}

此变体可以编译并运行,但是我不知道这种方式正确吗,为什么IntelliJ IDEA在放置版本定义的行上显示错误:

'val Build_gradle.project: Project' can't be called in this context by implicit receiver. Use the explicit one if necessary

2 个答案:

答案 0 :(得分:2)

有几个问题,其中有一些详细信息:

在Gradle的最新版本中,执行此操作的方法是使用settings.gradlesettings.gradle.ktspluginManagement {}块。

在您的情况下,可能看起来像这样:

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)
        }
      }
    }
  }
}

答案 1 :(得分:0)

(交叉发布:source

显然,这在过去已经成为可能,如果过去不可能的话。 (几乎)来自docs

gradle.properties

helloPluginVersion=1.0.0

settings.gradle.kts

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

现在文档说build.gradle.kts应该是空的,但是我的测试表明您仍然需要在build.gradle.kts中使用它:

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

版本现在由settings.gradle.kts确定,因此由gradle.properties确定,这就是我们想要的...