Assigning exec output to a closure not working

I got lot’s of help from this topic:

And referencing the Gradle docs:
https://docs.gradle.org/3.5/dsl/org.gradle.api.tasks.Exec.html

I tried to get the example from the aforementioned topic working with the gradle docs recommendation:

The following works (but note the commented section):

task task1(type: Exec) {
    commandLine "curl",  "-k", "www.google.com"
    println "task1 calling ${commandLine}"
	  standardOutput = new ByteArrayOutputStream()
    //ext.taskOutput = {
    //  return standardOutput.toString()
    //}
	  doLast {
  		ext.taskOutput = standardOutput.toString()

  		println "task1 command finished"
  		println "********************"
  	}
}

task task2(dependsOn: task1) {
	doFirst {
   		println "inside task2 - task1 command output is -- ${task1.taskOutput}"
  	}
}

This does not work - are the gradle docs incorrect or is there a trick I’m missing?

task task1(type: Exec) {
    commandLine "curl",  "-k", "www.google.com"
    println "task1 calling ${commandLine}"
	  standardOutput = new ByteArrayOutputStream()
    ext.taskOutput = {
      return standardOutput.toString()
    }
	  doLast {
  		//ext.taskOutput = standardOutput.toString()

  		println "task1 command finished"
  		println "********************"
  	}
}

task task2(dependsOn: task1) {
	doFirst {
   		println "inside task2 - task1 command output is -- ${task1.taskOutput}"
  	}
}

And I tried add the taskOutput to the closure in doLast. That also does not work.

I am able to do what I need with the working example above but I’m curious and a little frustrated that I could not use the docs to solve my problem. So I thought I’d post it up and either highlight a defect or help others in the future.

In your second (non-working) example, taskOutput is a closure, so it needs to be called to return its value:

println "inside task2 - task1 command output is -- ${task1.taskOutput()}"