2017-08-15 127 views
1

我有一個副本任務設置如下:搖籃排除模塊複製任務

task copyToLib(type: Copy) { 
    into "$buildDir/myapp/lib" 
    from configurations.runtime 

    // We only want jars files to go in lib folder 
    exclude "*.exe" 
    exclude "*.bat" 
    exclude "*.cmd" 
    exclude "*.dll" 

    // We exclude some lib 
    exclude group: "org.slf4j", name: "slf4j-api", version: "1.6.2" 
} 

而且我發現了以下錯誤:

Could not find method exclude() for arguments [{group=org.slf4j, name=slf4j-api, version=1.6.2}] on task ':copyToLib' of type org.gradle.api.tasks.Copy 

我有,這只是一個語法的感覺問題,任何提示?

+1

複製用於複製文件。所以你可以排除文件。您正在傳遞地圖。以下是您可以使用的各種排除方法的文檔:https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:排除(groovy.lang.Closure) –

+0

很感謝。所以,如果我理解得很好,下面應該做的伎倆:排除{it.file in configurations.runtime.files {it.name.equals(「slf4j-api」)}}。我再也沒有收到任何錯誤,但資源仍然包含在內...... – hublo

+0

不,它應該看起來像'exclude「slf4j-api.jar」',或'exclude {it.file.name.contains('slf4j -api')}'。運行時配置中沒有名爲slf4j-api的文件。 –

回答

2

按組排除:exclude group: org.slf4j

通過模塊排除:exclude module: slf4j-api

按文件名排除:exclude { it.file.name.contains('slf4j-api') }

排除文件:exclude "slf4j-api.jar"

您可以按組和模塊排除,但它需要進入配置排除這樣。然後它會在複製之前限制配置。

task copyToLib(type: Copy) { 
    into "$buildDir/myapp/lib" 
    from configurations.runtime { 
     exclude group: 'org.slf4j' 
    } 

    // We only want jars files to go in lib folder 
    exclude "*.exe" 
    exclude "*.bat" 
    exclude "*.cmd" 
    exclude "*.dll" 

} 

要記住,確保該目錄存在$buildDir/myapp/lib

也許不是排除所有其他文件只包含JAR和?

+1

感謝LazerBanana,它的作品像一個魅力!是的,100%同意,一個包括而不是一個exculde會好得多。乾杯!! – hublo

0

也許寫一個方法來幫助你

static String generateExcludeJar(Map<String, String> map) { 
    "$map.name-${map.version}.jar" 
    // All jar names are like name-version.jar when downloaded by Gradle. 
} 

exclude generateExcludeJar(group: "org.slf4j", name: "slf4j-api", version: "1.6.2")

+0

謝謝@aristotll。是的,這是可行的,但對我來說最好的辦法是根據其組或artifatId排除一個jar。但是,正如Jb Nizet所提到的,副本只有**(到目前爲止我瞭解)與文件,所以也許你的建議實際上是唯一的可能性。 – hublo