79146165

Date: 2024-10-31 19:59:35
Score: 2.5
Natty:
Report link

With @Cisco's comment I started to dig more into the lifecycle of Gradle. After digging in a bit, I found afterEvaluate - Adds a closure to call immediately after this project is evaluated. This is too late in the lifecycle to modify the executing tasks.

What I wanted to do was conditionally execute a tasks provided by a plugin after the 'build' task had completed. In case anyone else is interested, here is the process.

Step 1: Update the task I want to conditionally run to be conditional.

tasks.getByName("artifactoryPublish").onlyIf {
    properties["isPublishingEnabled"].toString().toBooleanStrict()
}

This will check the isPublishingEnabled property, and run the artifactoryPublish task only if the property is true.

Step 2: Attach it to the build.

tasks.getByName("build").finalizedBy("artifactoryPublish")

This directs tells gradle to run artifactoryPublish once build completes.

I had inherited the afterEvaluate block, and I have no idea why it was included. So, if anyone can explain how it could assist in this scenario, I would appreciate it.

Reasons:
  • Blacklisted phrase (1.5): would appreciate
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Cisco's
  • Self-answer (0.5):
Posted by: eimmer