How to skip a related copy task if the task it depends on is skipped?

Hello,

I need help with the following script, please. I have Copy tasks which are supposed to save log files created by dynamic tasks. The execution of the dynamic tasks are evaluated during execution (in the ME substituted by false). If the dynamic task is skipped I want to skip the related copy task as well.

Current behaviour is that all copy tasks are executed always - no matter what.

Background: I need to rerun a compilation of the same source code because auxillary files are changing during compilation (LaTeX).

project.ext {
    inputFile = 'source.src'
    outputFile = 'binary.bin'
    commandlineCommand = 'pdflatex'
}

def compileTasks = []

5.times { counter ->
    task "task$counter"(type: Exec) {
        inputs.source project.inputFile
        outputs.file project.outputFile
        doFirst {
            commandLine project.commandlineCommand, project.inputFile
        }
        onlyIf {
            false    // <-- For claryfication substituted with 'False' 
        }
    }
    task "saveLog$counter"(type: Copy) {        
        from('.') {
            include '*.log'
        }
        into "logSave/run_$counter"

        dependsOn "task$counter"
    }
    compileTasks.add tasks["task$counter"]
}

saveLog4.dependsOn saveLog0, saveLog1, saveLog2, saveLog3

How can I skip the saveLogX tasks?

I would like to have a solution which has a ‘programmatic style’: Maybe adding in the Copy tasks something in the style of

onlyIf "task$counter".state.executed.true

Thank you!

Thomas

Edit: changed Typo in code.

You could try:

onlyIf { "task$counter".didWork }

https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:didWork

In this case, I would also suggest just wrapping this into a custom task class that does the exec and copy in one go. If it doesn’t make sense to run the saveLog tasks separately, that would be a better solution.