2017-03-10 53 views
0

我對gradle比較新,所以這可能是一個典型的newbee問題。Gradle:從現有的戰爭中刪除一些文件|對於每個戰爭文件做:解壓縮,刪除/過濾器,組裝戰爭

在我們的gradle構建中,我們有一組war文件(依賴關係),它們都包含一個我們需要從構建我們的ear文件之前從這些war文件中刪除的文件。

如何能夠做到以下幾點:

- for all war files in a folder, 
    - extract war content to a location (using a Copy task & zipTree) 
    - re-pack to a new war applying a filter (using War & excludes) 

我想我將創建一個新的任務,並添加一些「dependsOn」聲明。

task excludeUnwantedFiles(){ 

    file(directoryWithOriginalWars).eachFile { file -> 
     ???? unpack war, filter, assemble new war file ???? 
    } 
} 

ear.dependsOn(excludeUnwantedFiles) 
excludeUnwantedFiles.dependsOn(downloadAllOriginalWarsIntoDirectory) 

如何創建該任務,執行每個戰爭文件?做這個的最好方式是什麼?

有一種方法可以在一項任務中完成此任務嗎?例如。複製任務並使用zipTree(fooBarWithFile.war)作爲'from'和'war(fooBarWithoutFile.war)',並在兩者之間應用過濾器?

或者這是要走的路,只是一個循環? Delete/Remove file from war with Gradle

任何幫助,高度讚賞! 乾杯, d。

--------- UPDATE -------------------

感謝名單蘭斯的Java爲您的解決方案。

正如我在我的評論中提到的,我面臨的問題是戰爭文件在執行期間被下載/提取,因此無法在配置時定義新任務。

我的解決方法是使用tarTree(帶過濾器)來訪問尚未提取的war文件列表。請參閱下面我的代碼示例:

def warFileSourceTarGz = '...tar.gz' 
def nfsLibDir="$buildDir/dependencies/" 
def nfsLibDownloadDir="$buildDir/downloadedDependencies/" 

// task that downloads & extracts the tar.gz 
task fetchNfsDependencies(type: Copy) {    
    from tarTree(warFileSourceTarGz) 
    into nfsLibDownloadDir  
} 


// loop through all war files inside the tar.gz and 
// create a task to remove unwanted libraries for each war 
task excludeUnwantedJarsFromWars(dependsOn: fetchNfsDependencies){ 

    // access the "remote" tar.gz file to get the list of war-files to loop over 
    def warFileSource = tarTree(warFileSourceTarGz).matching{ 
      include '*.war' 
    } 

    // for every war-file, create an exclude-path  
    warFileSource.visit { nextWarFile -> 

     if(nextWarFile.name.endsWith('.war')) { 

      String taskName = "excludeUnwantedJarsFrom_${nextWarFile.name.replace('.war', '')}" 
      String nextWarFilePath = nfsLibDownloadDir+"/"+nextWarFile.name 

      Zip tweakWarTask = tasks.create(name: taskName, type: Zip, dependsOn: fetchNfsDependencies) { 

       from zipTree(nextWarFilePath) 
       destinationDir = file(nfsLibDir) 
       archiveName = nextWarFile.name 

       // exclude these jars, as they cause classloading problems in our ear deployment. 
       exclude 'WEB-INF/lib/jcan-optrace*' 
      }  

      // hook into build-process 
      ear.dependsOn(tweakWarTask) 
     } 
    } 
} 

ear.dependsOn(excludeUnwantedJarsFromWars) 

回答

1

我具有assemble創建每個war文件和線材的所有任務到DAG中的Zip任務依賴於它們的所有

FileTree warFiles = fileTree(dir: 'path/to/wars', includes: ['**/*.war']) 
warFiles.files.each { File warFile -> 
    String taskName = "tweakWar${warFile.name.replace('.war', '')}" 
    Zip tweakWarTask = tasks.create(name: taskName, type: Zip) { 
     from zipTree(warFile) { 
      exclude 'path/to/some/file.xml' 
     } 
     destinationDir = "$buildDir/tweakedWars" 
     archiveName = warFile.name 
    } 
    // wire the task into the dag 
    assemble.dependsOn tweakWarTask 
} 
+0

Thaks很多關於這個建議。我意識到我忘了提到我想要處理的戰爭文件是在構建過程中下載的。所以上面的解決方案只適用於第二個exceution =>當戰爭文件已經存在於文件夾中時。我怎樣才能讓生成的任務在同一個版本中創建和執行? – Dominik

+0

您可以創建一個新的配置(例如'wars')並將戰爭依賴關係添加到'wars'配置中。您可以在我的示例中迭代'configurations.wars'而不是'fileTree' –