Gradle - Syntax error using plugin method with closure as argument

邮差的信 提交于 2021-01-29 04:50:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!