Freewind
Freewind

Reputation: 198258

Gradle Sync task with doLast doesn't work

The gradle task is working well:

task mySync1(type: Sync) {
    from "source-dir1"
    from "source-dir2"
    into "target-dir"
}

But if I add doLast, it doesn't nothing(and no errors):

task mySyncNotWorking(type: Sync) {
    doLast {
        from "source-dir1"
        from "source-dir2"
        into "target-dir-z"
    }
}

The correct one is:

task mySyncFixed() {
    doLast {
        project.sync {
            from "source-dir1"
            from "source-dir2"
            into "target-dir-z"
        }
    }
}

My question is the task mySyncNotWorking, the from and into methods are still belong to Sync if they are inside doLast? Why do they not work?

Upvotes: 0

Views: 583

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14503

Why do they not work?

They do work, but after the task was executed. And you can't configure something after it was executed.

If you really need to configure your task during execution phase, maybe because you need to use the results of other tasks (but can't use task outputs for some reason), just use a doFirst closure.

The reason why nothing happens in your second example is the nothing-to-do check of the implemented task action. It is perfectly fine for a task to do nothing, this is not a reason for an error.

Upvotes: 2

Related Questions