2017-02-22 72 views
0

在我的build.gradle文件中有一個javaexec任務,同時按順序處理超過100個文件。我想將增量構建行爲添加到此任務。 請讓我知道如何實現自定義JavaExec任務的增量功能。Gradle - 爲自定義javaexec任務添加增量行爲

+0

你好,歡迎來到StackOverflow。請花一些時間閱讀幫助頁面,尤其是名爲[「我可以詢問什麼主題?」(http://stackoverflow.com/help/on-topic)和[「我應該問什麼類型的問題避免問?「](http://stackoverflow.com/help/dont-ask)。更重要的是,請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/q/156810/204922)。您可能還想了解[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – languitar

回答

0
I have tried the following: 

class IncrementalReverseTask extends DefaultTask { 
    @InputDirectory 
    def File inputDir 

    @OutputDirectory 
    def File outputDir 

    @Input 
    def inputProperty 

    @TaskAction 
    void execute(IncrementalTaskInputs inputs) { 
     println inputs.incremental ? "CHANGED inputs considered out of date" 
            : "ALL inputs considered out of date" 
     if (!inputs.incremental) 
      project.delete(outputDir.listFiles()) 

     inputs.outOfDate { change -> 
      println "Modified: ${change.file.name}" 
      def targetFile = new File(outputDir, change.file.name) 
      targetFile.text = change.file.text.reverse() 
     } 

     inputs.removed { change -> 
      println "removed: ${change.file.name}" 
      def targetFile = new File(outputDir, change.file.name) 
      targetFile.delete() 
     } 
    } 
} 

then create a task like this 

task incrementalReverse(type: IncrementalReverseTask) { 
    inputDir = file('resource/inputs') 
    outputDir = file('resource/outputs') 
    inputProperty = project.properties['taskInputProperty'] ?: "original" 
} 

The above incremental task checks for changes in files in inputs folder.