Gradle-使用带有闭包作为参数的插件方法的语法错误 [英] Gradle - Syntax error using plugin method with closure as argument

查看:76
本文介绍了Gradle-使用带有闭包作为参数的插件方法的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是两个任务 hello & build.gradle 中的 printProperties :

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


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


Why plugins({id('java')}) give groovy scripted syntax error?

推荐答案

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

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

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.

这篇关于Gradle-使用带有闭包作为参数的插件方法的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆