Gradle exec in a while loop

I am attempting to do a gradle exec within a loop (see below). The loop occurs (infinitely I might add), but the command does not seem to execute. I’ve run the command itself in command line and it does run successfully. The println’s are there so I can see what is happening. The two println’s within the loop print continually.

How can I make sure the executable actually executes?

//loop until all instances have stopped completely task stopVerify(dependsOn: stopInstances) << {

def diff = matches.size()

println ("diff: " + diff)

while (diff > 0) {

println "difference: " + diff

def stoppedInstances = []

exec {

workingDir ‘/opt/aws/apitools/ec2/bin’

executable “./ec2-describe-instances”

args("–filter",“tag:Level=” + DeployLevel,"–filter",“tag:Type=Application”,"–filter",“instance-state-name=stopped”)

standardOutput = new ByteArrayOutputStream()

def outAsString = standardOutput.toString()

def matchInstanceId = outAsString =~ /INSTANCE\t(i.*)\tami/

matchInstanceId.each {stoppedInstances.add(it[1])}

diff = matches.size() - stoppedInstances.size()

println "Stopped instances: " + stoppedInstances.size()

}

} }

Any Ideas?

I found my own answer. The code inside the exec’s closure should only contain the definition for the exec. Doing it the following way gives me what I’m looking for:

//verify all instances are stopped. Loop until all instances have fully stopped task stopVerify(dependsOn: stopInstances) << {

def diff = matches.size()

while (diff > 0) {

def stoppedInstances = []

def outAsString

new ByteArrayOutputStream().withStream { os ->

exec {

workingDir ‘/opt/aws/apitools/ec2/bin’

executable “./ec2-describe-instances”

args("–filter",“tag:Level=” + DeployLevel,"–filter",“tag:Type=Application”,"–filter",“instance-state-name=stopped”)

standardOutput = os

}

outAsString = os.toString()

}

def matchInstanceId = outAsString =~ /INSTANCE\t(i.*)\tami/

matchInstanceId.each {stoppedInstances.add(it[1])}

diff = matches.size() - stoppedInstances.size()

println stoppedInstances.size() + " of " + matches.size() + " instances stopped."

} }

Hope this can be of use to someone else.

Thanks. In the future, please enclose your code within a html-style <code/> tag to enhance readability.