Gradle复制任务第一次没有从临时文件夹复制文件 [英] Gradle copy task not copying files from temp folder first time around

查看:13
本文介绍了Gradle复制任务第一次没有从临时文件夹复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行这样的任务的构建文件.

I've a build file that runs tasks like this.

任务 1 (unpackWar):将 war 文件解压缩到 Temp 文件夹

Task 1 (unpackWar): Unzips war file to Temp folder

任务 2 (copyWarFilesToWebContent):将文件复制到带有一些排除项的 WebContent 文件夹

Task 2 (copyWarFilesToWebContent): Copies the files to WebContent folder with some exclusions

任务 3 (copyRequiredJarFilesToWebContent):将几个 jar 文件从 Temp/WEB-INF/lib 解压缩到 TempJarDir

Task 3 (copyRequiredJarFilesToWebContent): Unzips a couple of jar files from Temp/WEB-INF/lib to TempJarDir

任务 4 (explodeProductJars):将我们想要的文件从 TempJarDir 复制到 WebContent 文件夹

Task 4 (explodeProductJars): Copies files we want from TempJarDir to WebContent folder

有一个prepare 任务使用dependsOn 运行这些任务中的每一个,并且我已将mustRunAfter 命令添加到每个任务中,以便它们按顺序执行.还要为每个任务设置 upToDateWhen = false.

There is a single prepare task that runs each of these tasks using dependsOn and I've added mustRunAfter commands to each of the tasks so they execute in order. Also set upToDateWhen = false for each task.

似乎发生的事情是任务 1 运行良好并解压缩了战争.然后任务 2 使用来自 Temp 的文件并将所需的文件正确添加到 WebContent.

What seems to happen is that Task 1 runs fine and unzips the war. Task 2 then uses the files from Temp and adds the required ones to WebContent correctly.

任务 3 和任务 4 总是以最新状态返回,因为在指定的目录中似乎没有文件可以使用.

Task 3 and Task 4 are always coming back as Up To Date because seemingly there are no files to work with in the directory specified.

如果我在 Temp 文件夹存在时重新运行准备,任务 3 和 4 将正确运行.

If I re-run prepare when the Temp folder exists, Task 3 and 4 run correctly.

我不确定这是由于 fileTree 的工作原理还是我做错了什么.我大约一周前拿起了 gradle,现在仍在掌握它.

I'm not sure if this is due to how fileTree works or if I'm doing something wrong. I've picked up gradle about a week ago and am still getting to grips with it.

任务如下所示:

task prepare(dependsOn: ['unpackWar', 'copyWarFilesToWebContent', 'copyRequiredJarFilesToWebContent'])
prepare.outputs.upToDateWhen {false}



task unpackWar(type: Copy) {
    description = 'unzips the war'
    outputs.upToDateWhen { false }


    def warFile = file(warFileLocation) 
    from zipTree(warFile)
    into "Temp"
}


task copyWarFilesToWebContent(type: Copy) {
    mustRunAfter unpackWar
    description = 'Moves files from Temp to WebContent Folder'
    outputs.upToDateWhen { false }

    from ('Temp') {
        exclude "**/*.class"
    }
    into 'WebContent'
}

task explodeProductJars(type: Copy) {
    outputs.upToDateWhen { false }
    FileTree tree = fileTree(dir: 'Temp/WEB-INF/lib', includes: ['application*-SNAPSHOT-resources.jar', 'services*-SNAPSHOT-resources.jar'])

    tree.each {File file ->
        from zipTree(file)
        into "TempJarDir"
    }
}

task copyRequiredJarFilesToWebContent(type: Copy, dependsOn: explodeProductJars) {
    mustRunAfter copyWarFilesToWebContent
    outputs.upToDateWhen { false }


    from ("TempJarDir/META-INF/resources") {
        include '**/*.xml'
    }
    into "WebContent/WEB-INF"

}

我感觉它与 fileTree 有关,但不确定到底发生了什么.

I've a feeling its something to do with fileTree but not sure whats happening exactly.

推荐答案

复制任务很棘手.复制任务只有在在配置阶段找到要复制的内容时才会执行.如果在该阶段没有找到任何东西,它将被跳过.

The Copy task is tricky. A Copy task will only be executed when it finds something to copy in the configuration phase. If it does not find anything during that phase it will be skipped.

您可以使用 复制方法 而不是复制任务.

You could use the copy method instead of the Copy task.

prepare( dependsOn: 'copyRequiredJarFilesToWebContent' ) {}

task unpackWar( type: Copy ) {

    def warFile = file( warFileLocation ) 
    from zipTree( warFile )
    into 'Temp'
}

task copyWarFilesToWebContent( dependsOn: unpackWar ) << {

    copy {
        from ( 'Temp' ) {
            exclude '**/*.class'
        }
        into 'WebContent'
    }
}

task explodeProductJars( dependsOn: copyWarFilesToWebContent ) << {

    copy {
        FileTree tree = fileTree( dir: 'Temp/WEB-INF/lib', includes: [ 'application*-SNAPSHOT-resources.jar', 'services*-SNAPSHOT-resources.jar' ] )

        tree.each { File file ->
            from zipTree( file )
            into 'TempJarDir'
        }
    }
}

task copyRequiredJarFilesToWebContent( dependsOn: explodeProductJars ) << {

    copy {
        from ( 'TempJarDir/META-INF/resources' ) {
            include '**/*.xml'
        }
        into 'WebContent/WEB-INF'
    }
}

这篇关于Gradle复制任务第一次没有从临时文件夹复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆