Adam
Adam

Reputation: 36703

How to run a task after another using taskGraph.whenReady closure

I'm trying to include a ZIP file inside a TAR file in a gradle build. I'm not insane, this is to replicate an existing ant script and I cannot change the distribution layout for various business reasons.

This is the general layout I have

task buildZip(type: Zip) {
   gradle.taskGraph.whenReady {
      // build zip file usual way with from blocks
      from(...) {
      }
      from(...) {
      }
   }
   doLast {
      println "ZIP ready"
      // could I call tar task from here??
   }
}

task buildTar(type: Tar, dependsOn: buildZip) {
    println "Building TAR"
    from (buildZip.archivePath) {
    }
    ... more stuff, installer script etc.
}

Output I see with gradle :buildTar, i.e. the TAR builds before the ZIP is built.

Building TAR
ZIP ready

Update.

Perryn Fowler comment below identifies the issue correctly, it is based on my misunderstanding of execution vs configuration in gradle.

The Tar is not being built before the Zip, the Tar task is being configured before the Zip task is executed

Update.

This question is no longer necessary as the option duplicatesStrategy can be used in the ZIP task to avoid the problem being 'fixed' with gradle.taskGraph.whenReady

Upvotes: 1

Views: 4299

Answers (2)

Adam
Adam

Reputation: 36703

The answer to this question was actually provided by Perryn Fowler in the top comments, it was was based on my misunderstanding of execution vs configuration in gradle. I've created this answer so the question is marked as answered. The other answer simply paraphrases the original question with a link to the userguide.

The Tar is not being built before the Zip, the Tar task is being configured before the Zip task is executed

i.e. any nested commands within a special task, e.g. Zip, Tar etc. are run and configuration time, the from blocks are executed later.

Upvotes: 2

Opal
Opal

Reputation: 84786

Here You have a sample working solution:

build.gradle:

task buildZip(type: Zip) {
    from 'dir'
    destinationDir project.file('build/zip')
    archiveName 'lol.zip'
}

task buildTar(type: Tar, dependsOn: buildZip) {
    from 'build/zip'
    include '*.zip'
    destinationDir project.file('build/tar')
    archiveName 'lol.tar'
}   

Is that clear for You?

P.S. I think that's a good idea for You to read userguide.

Upvotes: 1

Related Questions