Execute tasks within a task

I realize I am just being lazy, but I would call it being efficient. I would like to have a task that is not executed until I execute it through the command line or through the NetBeans IDE. This task will execute two other publish tasks. I am using Gradle 6.8.2, and upgrading to Gradle 7 would break other projects that have not been upgraded to deal with the breaking changes in Gradle 7 yet.

tasks.register("allQedPublishing") {
    dependsOn tasks.publishToMavenLocal
    dependsOn tasks.publishReleasePublicationToCloudsmithRepository
    System.out.println("QED Publishing to MavanLocal and Cloudsmith repositories.")

tasks.getByName("allQedPublishing").enabled = false;

This technically works, but the System.out.println is executed without having called it, and it is executed without calling the two publications listed in the dependOn statements. I assume this happens during the building of the task graph.

With that being said, I love Gradle. It is so much better than ant and maven because it is a programming language. How can I accomplish my goal without prematurely calling the println statement?

On a side note, is there a way to add line numbers to the code above? I guessed that 3 ` characters would create a code block. It would be handy in the future when discussing issues.

Thanks for the help in advanced.

That your println is executed is, because you do it at configuration phase, not execution phase.
To do it in execution phase, do it in a doLast { ... } block.

Setting enabled to false like you do is bad though.
You properly use tasks.register to leverage task configuration avoidance.
But then you use tasks.getByName which immediately realizes the task and configures it which also is the reason your println is executed.
If you want to set enabled to false, do it within the tasks.register closure.
But actually there is not much need to set enabled to false, as your task does not do any action anyway, I would just leave it out.

Regarding code block, using three backticks and after them the language is exactly right.
It seems Discourse does not support line numbers in code blocks out of the box: Is it possible to show line numbers in code block? - support - Discourse Meta

That changed worked. I am just using the println as a bit of information before the publication tasks are called. Right now, I am rely on both the dependsOn statement to execute the two publishing statements. Unfortunately, because I am using the dependsOn to call the two publishing tasks, the informational message is posted after the publishing is down instead of before it.

I have two further questions.

  1. Is there a framework in Gradle to post the informational message without calling the system out?

  2. Is there another way to execute the two tasks after I post the informational message?

tasks.register("allMyPublishing") {
    dependsOn tasks.publishToMavenLocal
    dependsOn tasks.publishReleasePublicationToCloudsmithRepository
    doFirst {
        System.out.println("Publishing to MavanLocal and Cloudsmith repositories.")
  1. Is there a framework in Gradle to post the informational message without calling the system out?


  1. Is there another way to execute the two tasks after I post the informational message?

Use finalizedBy instead of dependsOn.
finalizedBy is more for some cleanup task that should run after a given task even if that task failed.
But your task should anyway never fail as it prints just a message and that way your task will be executed before the other two tasks and thus should achieve what you want.

I tried to create the finalizeBy, but I keep getting an error. The code is:

def myPub = tasks.register("allPublishMy") {
    doLast {
        System.out.println("QED Publishing to MavanLocal and Cloudsmith repositories.")

myPub.configure( {
    finalizeBy publishToMavenLocal
    finalizeBy publishReleasePublicationToCloudsmithRepository

The error I am getting now is:

* Where:
Build file '/mnt/c/github/wasm/jwebassembly/JWebAssembly-Gradle/build.gradle' line: 176

* What went wrong:
A problem occurred configuring root project 'JWebAssembly-Gradle'.
> Could not create task ':allPublishMy'.
   > Could not find method finalizeBy() for arguments [task ':publishToMavenLocal'] on task ':allPublishMy' of type org.gradle.api.DefaultTask.

* Try:

Line 176 is the first finalizeBy line. Thanks.

You miss a d, that’s why. :smiley:

Thanks. That worked fine. I have no idea how I missed the d.

:heart: :heart: :heart: Tenacious D :heart: :heart: :heart:

1 Like