2015-12-08 82 views
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它只運行該任務,因爲它不依賴於其他任何人。

我在做什麼錯了?

回答

3

這是怎麼一回事呢:

  1. 所有腳本語句是爲了解析他們宣佈。
  2. apply plugin: BootstrapPlugin被執行。
  3. 當時BootstrapPlugin被應用bootstrapConfig閉包尚未被解析。由於它沒有被解析,因此BootstrapPlugin對配置一無所知,並對空集合進行迭代。
  4. bootstrapConfig當閉包無用時,它將被解析。

的解決方案是afterEvaluate塊來創建動態任務:

apply plugin: BootstrapPlugin 

bootstrapConfig { 
    basedir = 'C:/temp/unzip' 
    shareUrl = 'http://lol.com' 
    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.afterEvaluate { 
     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 
    String shareUrl 
    def componentList = [] 
} 

現在的任務確實存在,而且可以運行,但是他們不會與gradle tasks上市。

Here是演示。

+0

很好,它清楚地解釋了問題並且正常工作。非常感謝! – Mikyjpeg

相關問題