Copy files ind task which dependsOn - outputs not copied

Hi,

my buildscript looks like this:

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'
      sourceCompatibility = 1.8
    version = '1.0'
      project.ext {
        configDir = projectDir.getPath() + "/config"
        scriptDir = projectDir.getPath() + "/scripts"
    }
      repositories { mavenCentral() }
      dependencies {
        compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    }
  }
  task installApllication (dependsOn: subprojects.jar) {
    copy {
        from project(":sub1").configurations.runtime
        into buildDir.getPath() + '/application/lib'
    }
    copy {
        from project(":sub2").configurations.runtime
        into buildDir.getPath() + '/application/lib'
    }
    copy {
        from
subprojects.configDir
        into buildDir.getPath() + '/application/config'
    }
    copy {
        from
subprojects.scriptDir
        into buildDir.getPath() + '/application'
    }
    copy {
        from subprojects.jar.outputs
        into buildDir.getPath() + '/application'
    }
}

This executes all subproject.jar tasks and aggregates results in the parrent. The libs, scripts, and configs are copied just fine. But the .jar’s are not. They only get copied in the second pass through. So if i execute the task twice they get copied. But the older version of the jar is getting copied which means in a normal development process calling this will put me old jars into the folder. This means i have to execute it directly 2 times in a row to get the desired results.

At first I expected this should work but it seems like the jar.outputs is empty or old in the first pass and does not get updated due to the configurations lifecycles. Can anyone please help me with this? I tried putting the copies in a do last which results in the same behaviour. Also i wrapped the hole task in a doLast which is still the same.

The ‘installApplication’ task makes the common mistake of performing its work in the configuration rather than the execution phase. To fix this, the whole body of the task needs to be wrapped with ‘doLast { … }’. An even better solution is to use a task of ‘type: Copy’ (or, in this case, probably ‘Sync’), rather than an ad-hoc task that calls ‘project.copy’. Try:

task installApplication(type: Sync) {
  into "$buildDir/application"
  from subprojects*.scriptDir
  from subprojects*.jar
  into("config") {
    from subprojects*.configDir
  }
  into("lib") {
    from project(":sub1").configurations.runtime
    from project(":sub2").configurations.runtime
  }
}

Thanks for the sollution its working. I actually had a copy task before and I don’t know why it didn’t worked i just tried it again and it does mh…

But well can you please explain why the jar tasks are invoked with this your method but not in my? And where is the difference in using subproject.jar and subprojectjar.outputs?