Gradle - 使用以闭包作为参数的插件方法的语法错误



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

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')})给出时髦的脚本语法错误?

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

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

最新更新