【问题标题】:Gradle - Syntax error using plugin method with closure as argumentGradle - 使用带有闭包作为参数的插件方法的语法错误
【发布时间】:2019-12-27 23:44:29
【问题描述】:

以下是helloprintProperties 中的两个任务build.gradle

task('hello', {
            description("Hey student! Run this one :D")
            group("Our demo")
            doLast({println("Hello World!")})
            }
     )

plugins({
    id('java')
})

ext({
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
})

sourceSets.all({ ext.purpose = null })

sourceSets({
    main({
        purpose = "production"
    })
    test({
        purpose = "test"
    })
    plugin({
        purpose = "production"
    })
})

task('printProperties', {
    doLast({
        println(springVersion)
        println(emailNotification)
        sourceSets.matching({ it.purpose == "production" }.each({ println it.name }))
    })
})

给出错误:

> startup failed:
  build file '/../../build.gradle': 8:
only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

为什么plugins({id('java')}) 给出 groovy 脚本语法错误?

【问题讨论】:

  • 插件阻塞后定义任务?
  • @tim_yates 为什么开发人员使用声明式语法?它不可读...我无法理解带有声明性语法的单行 groovy 代码。真的很痛苦。你解决了上述错误。
  • 我不明白你在说什么????
  • 我认为他的意思是他发现删除了括号和逗号的 gradle dsl 很难阅读(与包含它们的问题相比)。恕我直言,这是您在 DSL 上花费了多少时间的函数。当您第一次开始时,您需要所有的逗号和括号。一旦你使用它几年,我必须在精神上解析的非必要、不贡献信息内容的字符越少越好。我也是一个clojure程序员。在clojure中,每个人都开始讨厌parens。五年后,我还没有听到一个人不喜欢他们。只是我的 2c。
  • @MatiasBjarland 花时间使用 DSL 与远离遵循另一种语法的常规编程语言(C/Python/java/GoLang/TyoeScript)是一样的,看起来有点接近脚本语法时髦的。

标签: java gradle groovy build.gradle


【解决方案1】:

答案在这里:https://stackoverflow.com/a/48611069/1250435

每当您编写 build.gradle 脚本并使用新插件时 脚本块,您需要将其作为文件中的第一个块。唯一的 此规则的例外是其他插件块或特殊的 buildScript 块,它总是必须先执行。

【讨论】:

    猜你喜欢
    • 2019-05-06
    • 2015-07-12
    • 2021-02-07
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2016-02-01
    相关资源
    最近更新 更多