【发布时间】:2019-12-27 23:44:29
【问题描述】:
以下是hello 和printProperties 中的两个任务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