2

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
Predz
  • 201
  • 1
  • 7
  • 17
  • 1
    Have you considered running gradle tasks with `--debug` or `--info` to see what does on? Maybe include the logs in your question? – frhd Nov 09 '16 at 21:08
  • I have run with --debug --info and --stacktrace. The messages are a bit cryptic in that from what I can recall it said that there were no files to process or something to that extent. Davids answer below kind of clarified why it wasn't seeing the files though. I believe its because when I run the task, the configuration phase processes each task at the same time and so the Temp folder didnt exist yet meaning the tasks were flagged as up to date. Open to correction on this though – Predz Nov 10 '16 at 11:52

1 Answers1

8

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'
    }
}
Flow
  • 23,572
  • 15
  • 99
  • 156
  • Hey, this solved my problems. Thanks for the help. I have a follow up question though which maybe you can answer. Is it possible to set includeEmptyDirs = false in some way with the copy method approach? – Predz Nov 10 '16 at 11:50
  • @Predz Glad it helped. You can simply add includeEmptyDirs = false to the task. For example right underneath into 'WebContent' will work. – David Schwarz Nov 10 '16 at 22:14