2016-03-09 43 views
2

在回答https://stackoverflow.com/a/35879150意義沒有在最後一行with什麼「與」在gradle這個

task gen (type: Jar) { 
    description "Generates JAR without version number." 
    archiveName = filename + ".jar" 
    manifest {attributes 'Main-Class': mainFile} 
    with jar 
} 

什麼確切含義,以及它在哪兒記錄?我無法在Gradle文檔中找到它,並且groovy中的with似乎有所不同。

回答

2

在你的情況下,你正在調用with() - Jar類的方法。 (請參見Jar DSL documentationJar API documentation的最下面)

將給定的規格添加爲此規格的子規格。

所以,這不是Groovy的with() - 方法。

1

對於這種情況下with你似乎調用一個名爲jar關閉:

在運行時(當構造器被調用)
jar { 
    baseName filename 
    manifest { 
     attributes 'Main-Class': mainFile 
    } 
} 

task gen (type: Jar) { 
    //.... 
    with jar 
} 

它會被轉換爲:

task gen (type: Jar) { 
    //.... 

    jar { 
    baseName filename 
    manifest { 
     attributes 'Main-Class': mainFile 
    } 
    } 
} 
+0

你有一個想法,有'帶'記錄?我試圖弄清楚如何學習gradle,但對於幾件事我找不到文檔,... – Edward

+0

'with'也記錄在您的鏈接中:http://groovy-lang.org/style-guide的.html#_using_with_for_repeated_operations_on_the_same_bean。關鍵是,'with'將閉包作爲參數並運行它。這與您在Gradle任務中看到的行爲完全相同。你可以在關閉時做幾乎所有的事情 – injecteer

+0

但是'jar'不是對Java Plugin生成的'Jar'類的實例的引用嗎?那麼它怎麼會是封閉的? – Edward