1
我正在開發一個gradle插件,當我嘗試動態創建任務時遇到問題。如何在gradle插件中動態創建任務?
這裏是我的代碼片段:
apply plugin: BootstrapPlugin
bootstrapConfig {
basedir = "C:/temp/unzip"
componentList = [
[
zipName: "test1",
zipVersion: "_2",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}"
],
[
zipName: "test2",
zipVersion: "_1",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}"
]
]
}
class BootstrapPlugin implements Plugin<Project> {
void apply(Project project){
project.extensions.create("bootstrapConfig",BootstrapConfigPluginExtension)
project.task("unzipAll", dependsOn: project.tasks.matching { Task task -> task.name.startsWith("unzipSingle")}){
println "DOIT!"
}
project.bootstrapConfig.componentList.each { element ->
project.task "unzipSingle${element.zipName}" (type: Copy) {
def artifactFile = new File(project.bootstrapConfig.shareUrl + "/" + element.zipNetworkLocation + "/" + element.zipName + element.zipVersion + ".zip")
def pathDestination = element.zipUnpackLocation + "/" + element.zipName + element.zipVersion
def touchFileName = pathDestination + "/" + element.zipName + element.zipVersion + ".pid"
from project.zipTree(artifactFile)
into pathDestination
}
}
}
}
class BootstrapConfigPluginExtension {
String basedir
def componentList = []
}
當我嘗試運行它的問題是,我試圖創建的任務沒有一個存在。運行unzipAll
它只運行該任務,因爲它不依賴於其他任何人。
我在做什麼錯了?
很好,它清楚地解釋了問題並且正常工作。非常感謝! – Mikyjpeg