Gradle:从现有战争中删除一些文件对于每个war文件do:unpack,remove / filter,assemble war

时间:2017-03-10 15:59:48

标签: gradle build.gradle

我比较陌生,所以这可能是一个典型的新问题。

在我们的gradle构建中,我们有一组war文件(依赖项),它们都包含我们需要从这些war文件中删除的文件,然后再构建我们的ear文件。

如何实现以下目标:

- 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)

如何创建为每个war-file执行的任务?这样做的最佳方式是什么?

我能在一项任务中采用这种方法吗?例如。使用Copy任务并使用zipTree(fooBarWithFile.war)作为'from'和'war(fooBarWithoutFile.war)'并在其间应用过滤器?

或者这是一种方式,只需一个循环? Delete/Remove file from war with Gradle

非常感谢任何帮助! 干杯, d。

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

Thanx Lance 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 个答案:

答案 0 :(得分:1)

我为每个war文件创建一个Zip任务,并通过assemble依赖所有任务将所有任务连接到DAG

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 
}