Custom Copy task copies outdated classes

I’m currently working on the migration of an Ant script to its corresponding Gradle version.
Everything works fine with the exception of a custom Copy task, which is in charge of copying the generated jar file from build/libs to another directory (out of the project).

If, after a first build, I introduce a change in any java file, and try to execute

gradle clean build deploy

i see the build/classes *.class files are correctly updated, and the build/libs/template-util.jar as well. However, the file template-util.jar copied to /home/fbudassi/Downloads has (the most of the times) the old version of the class. Sometimes, after some changes (I don’t really know where, because I’ve tried many many things), the copied jar is updated correctly.

This is my build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'

// Default task.
defaultTasks 'deploy'

// Configuration parameters.
group = 'com.rcs.template'
version = 'master'
sourceCompatibility = 1.7
targetCompatibility = 1.7

ext {
    // Manifest extra parameters.
    projectName = 'Template Util'
    vendor = 'Rotterdam Community Solutions'
    buildNumber = 10
    jarName = 'template-util.jar'
    // Dependencies versions.
    jacksonVersion = '2.6.0'

    // Deploy destinations.
    deployTo = ['/home/fbudassi/Downloads']

// Dependencies.
dependencies {
    compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
    compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "org.slf4j:slf4j-api:1.7.12"
    compile "org.apache.commons:commons-lang3:3.4"

    testCompile 'junit:junit:4.12'

repositories {

// Jar name and Manifest data.
jar {
    def buildDate = new Date().format("MMMMM dd yyyy")
    archiveName = jarName
    manifest {
    attributes 'Built-By': System.getProperty(''),
        'Built-Date': buildDate,
        'Built-JDK': System.getProperty('java.version'),        
        'Implementation-Title': projectName,
        'Implementation-Version': "$project.version - $buildDate - build: $buildNumber",
        'Implementation-Vendor': vendor

// Deploy task: copy jar to destinations.
task deploy(dependsOn: 'build') {
    deployTo.each { dest ->
        copy {
            from jar
            into dest

// Gradle Wrapper version.
task wrapper(type: Wrapper) {
    gradleVersion = '2.5'

Could you help me find an answer to the problem?

The problem is your deploy task is doing the copy as part of the task configuration and not as a task action. So it’s doing the copy at the beginning of the build before you’ve executed any task or created the new jar. So if there is an old jar there, he’s copying that. You’ll want to move that code into an action using either doFirst or doLast:

task deploy(dependsOn: 'build') {
    doLast {
        deployTo.each { dest ->
          copy {
            from jar
            into dest
1 Like

Thank you very much Gary!

After testing your solution (which worked perfectly well) and reading a little bit more the gradle users guide, I ended up using the << alternative, which seems to be a bit more succinct.