如何在Gradle复制任务中修改文件列表

如何在Gradle复制任务中修改文件列表

本文介绍了如何在Gradle复制任务中修改文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:摇篮新手...:-)

Warning: Gradle newbie... :-)

我正在将现有项目从Ant转换为Gradle.我的项目很大一部分由生成的代码组成.将此代码生成到内部目录中,并完成本地构建以确保正确生成代码.验证本地测试版本后,将生成的代码复制到一组WAR项目中以进行最终部署.

I'm in the process of converting an existing project from Ant to Gradle. A large portion of my project consists of code that is generated. This code is generated into an internal directory and a local build is done to ensure that the code is generated correctly. Once the local test build is verified, the generated code is copied to a set of WAR projects for final deployment.

因此,作为构建的一部分,我有一个任务将文件从测试构建复制到WAR项目.进行复制时,只有一些文件在目标WAR项目中尚不存在的情况下才应复制.为此,我编写了以下Gradle复制任务:

So as part of the build, I have a task that copies files from the test build to a WAR project. While making the copy there are a few files that should only be copied if they don't already exist in the destination WAR project. To do this, I wrote the following Gradle copy task:


task copyServer (type: org.gradle.api.tasks.Copy) {
    group = 'ds_copy'

    String serviceRootDir = "${DS_ROOT_DIR}/NDMS_DS_Generated_Services/server_src/${x_dsServiceDir}"
    String serviceDestDir = "${DS_ROOT_DIR}/NDMS_DS_Client/srv_src/${x_dsServiceDir}"

    FileTree setupFiles = fileTree ("$serviceRootDir") {
        include "gen/**/*.java", "request/gen/**.java"
    }

    doFirst {

        File appFile = new File (serviceDestDir, "${dsName}App.java")

        if (appFile.exists () == false)
        {
            setupFiles = fileTree (dir: "$serviceRootDir", include: ["gen/**/*.java", "request/gen/**.java", "${dsName}App.java"])
        }

        println ("Copying service classes for ${dsName} from ${serviceRootDir}")
        println ("Files to copy: ")
        setupFiles.each { println (it) }
        println (setupFiles)
    }

    from (setupFiles)

    into (serviceDestDir)

    doLast {
        println ("Finished copying service classes for ${dsName}")
    }
}

我的问题是我似乎无法从 doFirst 块中更新fileTree以便检查目标中文件是否存在.即使我看到 setupFiles 正在更新,但是当复制发生时,我在 doFirst 块中添加的文件也不会被复制.

My problem is that I can't seem to update the fileTree from the doFirst block in order to make the check for the existence of the files in the destination. Even though I see setupFiles getting updated, when the copy occurs the file I added in the doFirst block does not get copied.

有什么方法可以修改copyTask使用的fileTree吗?

Is there any way to modify the fileTree that the copyTask is using?

推荐答案

从不尝试从执行阶段更改任务的配置,尤其是在更改输入或输出的情况下.这将破坏最新的检查,我认为最新版本中的Gradle甚至积极禁止这样做.

Never ever try to change the configuration of a task from the execution phase, especially if this changes the inputs or outputs. This will break the up-to-date checks and I think Gradle in recent versions even actively prohibits this.

执行第二个任务来生成文件,然后使copyServer任务通过配置包含该任务的输出.如果操作正确,您甚至不需要显式的任务依赖关系,因为它应隐式添加.

Make a second task that generates your file and then make the copyServer task include the output of that task via configuration. You should not even need an explicit task dependency if you did it correctly, as it shoudl be added implicitly.

实际上,您的复制任务应该更像

Actually your copy task should more look like

task copyServer(type: Copy) {
    group = 'ds_copy'

    from serviceRootDir
    into serviceDestDir
    include 'gen/**/*.java'
    include 'request/gen/**.java'
    include "${dsName}App.java"

    doLast {
        println "Finished copying service classes for $dsName"
    }
}

顺便说一句.蚂蚁-> Gradle转换的一个技巧.学习Gradle,忘记您的Ant构建脚本,以Gradle的方式使用Gradle从头开始创建构建.如果您尝试移植现有的Ant构建,则很可能最终会看到非常丑陋的Gradle构建,其效率也可能大大低于它们.

Btw. one tip for the Ant -> Gradle conversion. Learn Gradle, forget your Ant buildscript, create your build from scratch with Gradle in the Gradle way. If you try to port your existing Ant build, you will most likely end up with very ugly Gradle builds that might also be much less efficient than they could be.

这篇关于如何在Gradle复制任务中修改文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:29