Gradle copy task not copying files from temp folder first time around

一笑奈何 提交于 2020-04-11 06:50:09

问题


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

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

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

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

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

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.

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.

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.

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

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.

Tasks look like this:

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"

}

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


回答1:


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'
    }
}


来源:https://stackoverflow.com/questions/40505629/gradle-copy-task-not-copying-files-from-temp-folder-first-time-around

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!