首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle:从现有的war中删除一些文件|对于每个war文件执行:解包,删除/过滤,组装war

Gradle:从现有的war中删除一些文件|对于每个war文件执行:解包,删除/过滤,组装war
EN

Stack Overflow用户
提问于 2017-03-10 23:59:48
回答 1查看 2.2K关注 0票数 0

我是gradle的新手,所以这可能是一个典型的newbee问题。

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

如何实现以下目标:

代码语言:javascript
复制
- 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”声明。

代码语言:javascript
复制
task excludeUnwantedFiles(){

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

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

如何创建为每个war文件执行任务?做这件事最好的方法是什么?

有没有办法让我在一个任务中做到这一点?例如,使用复制任务,使用zipTree(fooBarWithFile.war)作为'from‘和'war(fooBarWithoutFile.war)’,并在两者之间应用过滤器?

或者这是一种方法,仅仅使用循环?Delete/Remove file from war with Gradle

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

-更新

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

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

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-11 01:18:29

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

代码语言:javascript
复制
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 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42722644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档