Can't use project extra properties in plugin block

前端 未结 3 913
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 11:54

I have a multi-project build, and more often than not I find myself locking versions for artifacts across the board. So in my root project I define something like:

相关标签:
3条回答
  • 2020-12-20 12:16

    According to documentation (see Constrained syntax subsection)

    «plugin version» and «plugin id» must be constant, literal, strings

    There are some other notes related to your question in the following paragraphs:

    The plugins {} block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).

    Can only be used in build scripts

    The plugins {} block can currently only be used in a project’s build script. It cannot be used in script plugins, the settings.gradle file or init scripts.

    Future versions of Gradle will remove this restriction.

    So it is not possible to do this now.

    There is a workaround to extract the plugin version and use it afterwards, but I personally find it ugly and prefer using explicit versions.

    0 讨论(0)
  • 2020-12-20 12:21

    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...

    0 讨论(0)
  • 2020-12-20 12:23

    build.gradle.kts root

    buildscript {
      val kotlinVersion by rootProject.extra { "1.3.10" }
      ...
    }
    

    build.gradle.kts module

    dependencies {
      implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootProject.extra.get("kotlinVersion")}")
    }
    

    Official docs

    0 讨论(0)
提交回复
热议问题