# Command line task

**URL:** <https://discuss.gradle.org/t/command-line-task/7280>\
**Category:** Old Forum Archive\
**Created:** [November 2, 2012, 9:33pm UTC](https://discuss.gradle.org/t/command-line-task/7280 "2012-11-02T21:33:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barrett\_Kern](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/barrett_kern/32/1175_2.png) [@Barrett\_Kern](https://discuss.gradle.org/u/Barrett_Kern)\
**Post date:** [November 2, 2012, 9:33pm UTC](https://discuss.gradle.org/t/command-line-task/7280/1 "2012-11-02T21:33:00Z")

</div>

I have a gradle task I wrote that runs something on the command line. The process takes ~15 seconds and writes its status to the console. I want to take action based on the status that is written to the console. I see the standardOutputStream property for an Exec task but anytime I println that it seems to print to the console first and then my process finally writes to the console. How do I handle this situation in gradle?

---

<div class="post-metadata">

**Author:** ![Seth\_Goings](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/seth_goings/32/162_2.png) [@Seth\_Goings](https://discuss.gradle.org/u/Seth_Goings)\
**Post date:** [November 5, 2012, 4:48am UTC](https://discuss.gradle.org/t/command-line-task/7280/2 "2012-11-05T04:48:00Z")

</div>

I’m not sure what exactly you’re exec’ing (which might have special output behavior that I’m not aware of)…

Could the process be sending things to std error that you’re not intercepting?

---

<div class="post-metadata">

**Author:** ![Barrett\_Kern](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/barrett_kern/32/1175_2.png) [@Barrett\_Kern](https://discuss.gradle.org/u/Barrett_Kern)\
**Post date:** [November 5, 2012, 12:08pm UTC](https://discuss.gradle.org/t/command-line-task/7280/3 "2012-11-05T12:08:00Z")

</div>

I am running my jasmine js unit tests via phantom js(headless webkit tool).

Here is my task definition on my local machine

```gradle
task runJasmine(type: Exec) {
    workingDir '/src/main/webapp/resources/js/tests/'
      //windows
     commandLine 'cmd', '/c', 'phantomjs jasmine_phantom.js SpecRunner.html results.html'
        println 'execResult ' + execResult
    println 'stdOut ' + standardOutput
    println 'stdIn ' + standardInput
}

```

Here is the output I am seeing where the printlns in the task seem to print to the console before the script that is running phantom does.

execResult null stdOut org.apache.commons.io.output.CloseShieldOutputStream@1ef479c2 stdIn java.io.ByteArrayInputStream@4465bf6e :my-project:runJasmine

------------------------------------------------------- J A S M I N E

T E S T S -------------------------------------------------------

Test URL: SpecRunner.html Tests completed in 2408 ms Test results file: results.html Tests passed.

BUILD SUCCESSFUL

Total time: 19.377 secs

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [November 5, 2012, 1:00pm UTC](https://discuss.gradle.org/t/command-line-task/7280/4 "2012-11-05T13:00:00Z")

</div>

By default, forked processes write through to the parent processes stdout. This isn’t what you want. You want to collect the output and then parse it.

```gradle
task runJasmine(type: Exec) {
    workingDir '/src/main/webapp/resources/js/tests/'
    //windows
     commandLine 'cmd', '/c', 'phantomjs jasmine_phantom.js SpecRunner.html results.html'
   standardOutput = new ByteArrayOutputStream()
     doLast {
     String output = standardOutput.toString()
   }
}

```

---

<div class="post-metadata">

**Author:** ![Szczepan\_Faber](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/szczepan_faber/32/213_2.png) [@Szczepan\_Faber](https://discuss.gradle.org/u/Szczepan_Faber)\
**Post date:** [November 5, 2012, 1:12pm UTC](https://discuss.gradle.org/t/command-line-task/7280/5 "2012-11-05T13:12:00Z")

</div>

The execResult is null because the task hasn’t ran yet.

Printing the standardOutput/input they way you do it is not quite right because:

- you’re printing it at configuration phase

- printing it only prints the string representation of the default stream (by default, Gradle uses System.out). If you want to get hold of the output, assign brand new ByteArrayOutputStream. Take a look at the example:

```gradle
task foo(type: Exec) {
  commandLine = //
      //store the output instead of printing to the console
  standardOutput = new ByteArrayOutputStream()
      //extension method foo.output() can be used to obtain the output String
  ext.output = {
    return standardOutput.toString()
  }
  }
  task showFooOutput(dependsOn: foo) {
  doLast {
    println "Foo's output: ${foo.output()}"
  }
}

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Barrett\_Kern](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/barrett_kern/32/1175_2.png) [@Barrett\_Kern](https://discuss.gradle.org/u/Barrett_Kern)\
**Post date:** [November 5, 2012, 3:10pm UTC](https://discuss.gradle.org/t/command-line-task/7280/6 "2012-11-05T15:10:00Z")

</div>

@Luke - This version of the task worked @Szczepan - I don’t understand this example 100%. Where is ext referring to? What is it? When I lookup exec task I don’t see ext as property listed (perhaps its inherited?).

---

<div class="post-metadata">

**Author:** ![Seth\_Goings](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/seth_goings/32/162_2.png) [@Seth\_Goings](https://discuss.gradle.org/u/Seth_Goings)\
**Post date:** [November 5, 2012, 5:26pm UTC](https://discuss.gradle.org/t/command-line-task/7280/7 "2012-11-05T17:26:00Z")

</div>

ext refers to: [http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html](http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html)

---

<div class="post-metadata">

**Author:** ![Barrett\_Kern](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/barrett_kern/32/1175_2.png) [@Barrett\_Kern](https://discuss.gradle.org/u/Barrett_Kern)\
**Post date:** [November 6, 2012, 11:51am UTC](https://discuss.gradle.org/t/command-line-task/7280/8 "2012-11-06T11:51:00Z")

</div>

Thanks that was really helpful. I never knew it existed

---

<div class="post-metadata">

**Author:** ![Barrett\_Kern](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/barrett_kern/32/1175_2.png) [@Barrett\_Kern](https://discuss.gradle.org/u/Barrett_Kern)\
**Post date:** [November 6, 2012, 7:13pm UTC](https://discuss.gradle.org/t/command-line-task/7280/9 "2012-11-06T19:13:00Z")

</div>

Do you guys have any suggestions on how to fail the build if that text indicates failure?

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [November 6, 2012, 7:16pm UTC](https://discuss.gradle.org/t/command-line-task/7280/10 "2012-11-06T19:16:00Z")

</div>

You can just throw an exception.
