2016-06-07 75 views
3

我不明白,被排除在外,爲什麼我們不用引號添加到gradle這個任務的名稱,當我們把它聲明 喜歡:爲什麼引號的gradle這個任務名稱

task hello (type : DefaultTask) { 
} 

我我在一個常規項目中嘗試過,發現它是非法的,Gradle是如何工作的。 我不明白上面的表達式,爲什麼我們可以添加(type : DefaultTask),我們如何用常規語法分析它?

+3

的[理解在常規語法可能的複製gradle任務定義](https://stackoverflow.com/questions/27584463/understanding-the-groovy-syntax-in-a-gradle-task-definition) – tkruse

回答

2

如在groovyConsole中可運行形式的例子,你可以正是如此定義位代碼:

// 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' 
} 

所以執行底部的腳本時,它打印Hello tim到stdout

但大衛答案是正確的,這只是舉例

See also here in the documentation for Groovy

1

Gradle構建腳本是一個Groovy DSL應用程序。通過小心使用「methodMissing」和「propertyMissing」方法,所有的魔法都是可能的。

我不記得圍繞「任務」的確切機制。我認爲這是在Gradle論壇上提出的(可能不止一次)。

+0

機制在這裏評論:https://stackoverflow.com/問題/ 12326264 – tkruse

相關問題