为什么可以将引号放在gradle任务的名字中 [英] why can quotes be left out in names of gradle tasks

查看:165
本文介绍了为什么可以将引号放在gradle任务的名字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务hello(类型:DefaultTask){
}

I已经在一个常规项目中尝试过,发现它是非法的,Gradle是如何运作的。
我不明白上面的表达式,为什么我们可以添加(type:DefaultTask),我们如何用常规语法分析它?

解决方案

作为GroovyConsole可运行表单的示例,您可以这样定义一些代码:

  //设置我们DSL的基类

@BaseScript(MyDSL)
导入groovy.transform.BaseScript

//处理某人的东西
class Person {
字符串名称
Closure方法
String toString(){$ name}
Person( String name,Closure cl){
this.name = name
this.method = cl
this.method.delegate = this
}
def greet(String greeting ){
println$ greeting $ name
}
}

//和我们的基本DSL类

抽象类MyDSL扩展脚本{
def methodMissing(String name,args){
return new Person(name,args [0])


def person(Person p){
p.method(p)
}
}

//然后我们的实际脚本

person tim {
greet'Hello'
}

因此,当底部的脚本执行时,它将 Hello tim 输出到stdout中。

但大卫的答案是正确的,例如



另请参阅Groovy文档


I don't understand why we don't need to add quotes to the name of gradle task when we declare it like:

task hello (type : DefaultTask) {
}

I've tried in a groovy project and found that it's illegal, how gradle makes it works. And I don't understand the expression above neither, why we can add (type : DefaultTask), how can we analyze it with groovy grammar?

解决方案

As an example in a GroovyConsole runnable form, you can define a bit of code thusly:

// Set the base class for our DSL

@BaseScript(MyDSL)
import groovy.transform.BaseScript

// Something to deal with people
class Person { 
    String name
    Closure method
    String toString() { "$name" }
    Person(String name, Closure cl) {
        this.name = name
        this.method = cl
        this.method.delegate = this
    }
    def greet(String greeting) {
        println "$greeting $name"
    }
}

//  and our base DSL class

abstract class MyDSL extends Script {
    def methodMissing(String name, args) {
        return new Person(name, args[0])
    }

    def person(Person p) {
        p.method(p)
    }
}

// Then our actual script

person tim {
    greet 'Hello'
}

So when the script at the bottom is executed, it prints Hello tim to stdout

But David's answer is the correct one, this is just for example

See also here in the documentation for Groovy

这篇关于为什么可以将引号放在gradle任务的名字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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