0

I need to programatically clear the UP-TO-DATE property for gradle task dependencies. I am working in a subproject build.gradle file that handles the integration of other subprojects and deploys them. I need to be able to clear the property only if one specific task is running. I want to ensure that three of my tasks (clean, cleanNodeJs, and npmInstall) are always rerun prior to starting my deployment task. However, I don't want to affect other tasks where it is acceptable for it to not rerun those things (especially since npmInstall takes a non-trivial amount of time to run).

I looked at this similar question but the answers there do not work for me. Using --rerun-tasks doesn't suffice because it's dependent upon the caller to specify and I need to ensure that my required tasks are always rerun for this deployment task. Setting outputs.upToDateWhen { false } for those three tasks isn't what I'm looking for either as that would then require those tasks to always rerun even for other tasks that don't need them to (unless there's a way to specify that just within my deployment task?). I tried the 3rd suggested answer within the build.gradle file by prepending clean in my dependsOn (i.e. dependsOn(cleanClean) but that threw an error for an unrecognized task. Perhaps the fourth answer could work but I'm not sure I understand it.

I did try setting my deployment task to depend on the three aforementioned tasks and then specifying that each one must run after the appropriate one (see below) but that doesn't seem to help.

task deploy() {
    dependsOn(clean, cleanNodeJs, npmInstall)
    // my deployment logic
}
cleanNodeJs.mustRunAfter(clean)
npmInstall.mustRunAfter(cleanNodeJs)
deploy.mustRunAfter(npmInstall)

Hopefully what I'm trying to do is possible. Thanks in advance for your time and help!

Community
  • 1
  • 1

1 Answers1

1

As the upToDateWhen closure is evaluated after the task graph is built, you should be able to do something like outputs.upToDateWhen { !gradle.taskGraph.hasTask(deploy) }.

mustRunAfter does not add any dependencies. It just defines order if both tasks involved will be executed, but you can still execute both tasks without executing the other one.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Thanks for the helpful info. I'm going to accept this answer as I believe it could help others, however, my issue ended up being a result of me overlooking task name collisions. My deployment task name started with `check` and I was taking the shortcut of running `./gradlew check` rather than type the full task name. We already had a gradle task (in the parent `build.gradle` file) named `check` and so that's why I was not seeing my expected behavior. Our `clean` and `cleanNodeJs` tasks are never actually considered UP-TO-DATE so I don't even need to clear that property for those tasks. – user5235812 May 15 '17 at 16:50
  • Sorry, I'm trying to obey the rules but in this case it wasn't clear if I should supply the additional information as a comment or submit my own answer explaining what my actual problem was. I felt your answer would be more beneficial to future viewers than what my actual problem was so that's why I went the route I did. Unfortunately, this comment is probably breaking the rules now too :( – user5235812 May 15 '17 at 17:33
  • Well, I just asked for my upvote actually. :-D While I see, you cannot yet, you miss one reputation for being able to upvote. :-) – Vampire May 15 '17 at 17:51