问题
Below are two tasks hello
& printProperties
in 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 }))
})
})
that gives error:
> startup failed:
build file '/../../build.gradle': 8:
only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed
Why plugins({id('java')})
give groovy scripted syntax error?
回答1:
It's answered here : https://stackoverflow.com/a/48611069/1250435
Whenever you write a build.gradle script and use the new plugins script block, you need to put it as first block in the file. The only exceptions from this rule are other plugins blocks or the special buildScript block, which always must go first.
来源:https://stackoverflow.com/questions/59507030/gradle-syntax-error-using-plugin-method-with-closure-as-argument