Exec Task Dependent on Execution phase task?

Hello,

I am after some help with regards to using an exec task and it’s place in the lifecycle. We are trying to use the Git Gradle plugin to get the working branch name which is then used as part of an external execute (part of building a Delphi application). But we have some problems around the configuration and execution phases on the lifecycle. It could be because we’re just getting our hands truly dirty in the Gradle world.

Anyway, here is an example piece of code and some ideas on how to address this would be greatly appreciated:

task branchName(type: GitBranchList) {
    branchType = GitBranchList.BranchType.LOCAL
    project.ext["branch.name"] = "unknown"
    doLast {
        project.ext["branch.name"] = getWorkingBranch().name
    }
}
  task doSomeExecutable(type: Exec, dependsOn: ["branchName"]) {
    commandLine "some-executable.exe", project.ext["branch.name"]
}

The above works…but it only ever passes in “unknown” as the parameter. Because “getWorkingBranch” is an execution phase only function and the Exec task is configuration phase, we’re facing a chicken and egg issue here.

Is there an appropriate approach to this problem?

Regards,

Wade

Is there an appropriate approach to this problem?

Yes, there are several approaches. In this case, a configuration task seems most appropriate…

task branchName(type: GitBranchList) {
    branchType = GitBranchList.BranchType.LOCAL
    project.ext["branch.name"] = "unknown"
    doLast {
        project.ext["branch.name"] = getWorkingBranch().name
    }
}
  task configDoSomeExecutable {
    dependsOn branchName
    doLast {
      doSomeExecutable.commandLine "some-executable.exe", project.ext["branch.name"]
    }
}
  task doSomeExecutable(type: Exec) {
    dependsOn configDoSomeExecutable
 }
1 Like

Sorry for delayed reply (my Gradle work got sidelined by bigger needs).

Thanks for the suggestion worked a treat. Really enjoying Gradle the more I get into it.

Thanks again Luke.

that helped me as well.

Luke, would you mind telling us what other approaches are?

also I do believe I understand why your proposed approach works, but I am not 100% sure - would you mind explaining the lifecycle here?