【问题标题】:Can't set variables before plugins{}无法在插件之前设置变量{}
【发布时间】:2019-07-16 10:42:37
【问题描述】:

我有这个build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
    id 'eclipse-wtp'
}

[...]

dependencies {
    compileOnly group: 'org.springframework.boot', name: 'spring-boot-dependencies', version: '2.1.6.RELEASE', ext: 'pom'

[...]

我会指定一个变量springBootVersion = "2.1.6.RELEASE"。不幸的是,这是不可能的,因为我收到了这个错误:

只允许 buildscript {} 和其他插件 {} 脚本块 在插件 {} 块之前

我也尝试从spring-boot-dependencies 中删除该版本,但出现此错误:

无法解析:org.springframework.boot:spring-boot-dependencies

有没有办法在plugin{} 块之前声明变量,或者,从spring-boot-dependencies 中删除版本?

我正在使用 Gradle 5.4.1

【问题讨论】:

    标签: spring-boot gradle build.gradle


    【解决方案1】:

    您不能在插件块中使用变量。在此处检查“严格语法”部分:https://docs.gradle.org/current/javadoc/org/gradle/plugin/use/PluginDependenciesSpec.html 还: https://github.com/gradle/gradle/issues/3593

    除了插件块之外,您不需要在其他任何地方定义 spring boot 版本,方法是应用 spring boot 插件(org.springframework.boot),然后在块之后应用(io.spring.dependency-management) .然后后者将负责根据引导插件版本解析正确的版本。

    如果还不知道的话,我建议使用https://start.spring.io/来生成你的spring boot项目,因为它可以为你生成一个现成的gradle项目。

    【讨论】:

    • 是的,问题是我添加了spring-boot-dependencies,而我已经有了插件io.spring.dependency-management。无论如何,我不明白为什么我无法从spring-boot-dependencies 中删除该版本。
    【解决方案2】:

    也许

    plugins {
        ext.springBootVersion = '2.1.6.RELEASE' 
        id 'org.springframework.boot' version springBootVersion
        ... 
    }
    
    [...]
    
    dependencies {
        compileOnly "org.springframework.boot:spring-boot-dependencies:${plugins.springBootVersion}@pom"
    

    【讨论】:

    • 错误:only id(String) method calls allowed in plugins {} script block。您至少尝试过代码吗?
    • “您至少尝试过代码吗?”不,所以我说“也许”
    【解决方案3】:

    使用 buildscript 你可以做到这一点:

    buildscript {
      ext {
        springBootVersion = '2.6.6'
      }
    }
    plugins {
      id 'org.springframework.boot' version "${springBootVersion}"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      相关资源
      最近更新 更多