Because the doLast { ... } block in task1 isn’t evaluated until the execution phase the “loginCommand” property doesn’t exist during the configuration phase which is when task2 needs it to be defined.
If you move the ext.loginCommand = "hostname" line to be outside the doLast it will work as you want.
C:\MyData\projects\rdms_microservices\listxls>gradlew task2 -x test
Running command a.bat
Running command a.bat, method 2
:task2
inside task2 - login command output is -- ipconfig /a
inside task2 - login2 command output is -- ipconfig /a
:task2 FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':task2'.
> A problem occurred starting process 'command 'ipconfig /a''
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 6.439 secs
Some reason it fails, if we pass the arguments.
if i put “echo hostname” inside a.bat, it works, but doesnt work if i put “echo ipconfig /a” inside a.bat
Error from stack trace
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'ipconfig /a''
at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:197)
at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:327)
at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:86)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'ipconfig /a'
at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
at net.rubygrapefruit.platform.internal.WindowsProcessLauncher.start(WindowsProcessLauncher.java:22)
at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:68)
... 2 more
Caused by: java.io.IOException: Cannot run program "ipconfig /a" (in directory "C:\MyData\projects\rdms_microservices\listxls"): CreateProcess error=2, The system cannot find the file specif
at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
... 5 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
... 6 more
BUILD FAILED
Total time: 4.939 secs
task showId(type: Exec) {
commandLine 'whoami'
standardOutput = new ByteArrayOutputStream()
ext.hash = {
standardOutput.toString()
}
}
task writeHash(dependsOn: 'showId') {
def result = file("${buildDir}/hash.txt")
outputs.file result
doLast {
if(!buildDir.exists()) {
buildDir.mkdirs()
}
// Here you are referencing to var as tasks.{taskName}.{variableName} to read it. Because commandLine executes in the
// early LifeCycle we need to read var and execute inside doLast cycle
def hash = tasks.showId.hash()
println hash
result.write(hash)
exec {
commandLine ('echo','hash: ', "$hash")
}
}