【问题标题】:Gradle: remove some files from an existing war | for each war-file do: unpack, remove/filter, assemble warGradle:从现有的战争中删除一些文件|对于每个战争文件做:解包,删除/过滤,组装战争
【发布时间】:2017-03-10 15:59:48
【问题描述】:

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

如何创建为每个战争文件执行的任务?最好的方法是什么?

有没有办法在一项任务中做到这一点?例如。使用 Copy 任务并使用 zipTree(fooBarWithFile.war) 作为 'from' 和 'war(fooBarWithoutFile.war)' 并在两者之间应用过滤器?

或者这是要走的路,只是有一个循环? Delete/Remove file from war with Gradle

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

---------更新-------

感谢 Lance Java 为您提供解决方案。

正如我在评论中提到的,我遇到的问题是,war 文件在执行期间被下载/提取,因此无法在配置时定义新任务。

我的解决方法是使用 tarTree(带有过滤器)来访问尚未提取的战争文件列表。请参阅下面的代码示例:

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)

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    我会为每个 war 文件创建一个 Zip 任务,并将所有任务连接到 DAG 中,让 assemble 全部依赖它们

    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 
    }
    

    【讨论】:

    • 非常感谢这个建议。我意识到我忘了提到我要处理的战争文件是在构建过程中下载的。因此,上述解决方案仅适用于第二次执行 => 当文件夹中已经存在战争文件时。如何使生成的任务在同一个构建中创建和执行?
    • 您可以创建一个新配置(例如wars)并将战争依赖项添加到wars 配置中。然后,您可以在我的示例中迭代 configurations.wars 而不是 fileTree
    • 简短评论:当我尝试将 zipTree 与闭包,所以我不得不将“from zipTree(...).{exclude 'file'}”更改为“from zipTree(...).matching{exclude 'file'}”。添加“匹配”调用解决了它
    猜你喜欢
    • 2015-10-29
    • 2018-11-03
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多