2012-06-25 23 views

回答

3

目前,不存在Gradle插件來簡化我所知道的任務。我認爲如果你寫一篇文章並向社區貢獻,這將是非常棒的。現在你可以使用htmlcompressor提供的Ant tasks。在運行任務之前確保輸入和輸出目錄確實存在。依賴性定義中的版本限定符允許您通過使用加號來提取更新的版本,例如1.+。我不會推薦這樣做,因爲如果Ant任務定義更改爲新版本,它可能會破壞您的構建。

configurations { 
    htmlcompressor 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    htmlcompressor 'com.googlecode.htmlcompressor:htmlcompressor:1.4' 
} 

task compressHtml << { 
    ant.apply(executable: "java", parallel: false) { 
     fileset(dir: "test", includes: "*.html") { 
      exclude(name: "**/leave/**") 
     } 

     arg(value: "-jar") 
     arg(path: configurations.htmlcompressor.asPath) 
     arg(line: "--type html") 
     arg(value: "--preserve-comments") 
     srcfile() 
     arg(value: "-o") 
     mapper(type: "glob", from: "*", to: "compressed/*") 
     targetfile() 
    } 
} 

編輯:你實際上並不需要依賴添加到腳本的類路徑。使用它的配置更清潔。我改變了腳本以反映這一點。

+0

謝謝。我最終使用了類似的方法,但是使用Exec任務來執行它。我有點新手,但是一旦我有更多的經驗,我會着眼於編寫一個插件。 – Alistair

0

該jar應該從maven central加載,新版本應該在發佈時自動使用。

可以用下面的依賴來完成:

dependencies { 
    htmlcompressor 'com.googlecode.htmlcompressor:htmlcompressor:latest.integration' 
}