# How to run a CMD file in directory relative to the project

**URL:** <https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589>\
**Category:** Old Forum Archive\
**Created:** [November 19, 2013, 1:31pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589 "2013-11-19T13:31:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 19, 2013, 1:31pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/1 "2013-11-19T13:31:00Z")

</div>

I am trying to run the following task but it does not seem to run.

```gradle
task genOCFWUploadFiles(type:Exec) {
 workingDir './OCManualBuildFiles'
    commandLine 'cd'
 commandLine 'cmd', '/c', 'AA-RunAll.cmd'
    standardOutput = new ByteArrayOutputStream()
    ext.output = {
   return standardOutput.toString()
 }
 }

```

I am running the following tasks in order.

```gradle
:clean, :build, :buildDependents, :buildNeeded, :eclipse

```

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 19, 2013, 9:12pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/2 "2013-11-19T21:12:00Z")

</div>

From what I can see you are not requesting the task to be executed on the command line. Or did you declare it as task dependency for one of the tasks listed above?

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 2:56am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/3 "2013-11-20T02:56:00Z")

</div>

How do I make it a dependent task? Also how do I evoke it from the command line?

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 3:42am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/4 "2013-11-20T03:42:00Z")

</div>

I changed the above to:

```gradle
task genOCFWUploadFiles(type:Exec) << {
 workingDir './OCManualBuildFiles'
    commandLine 'cd'
 commandLine 'cmd', '/c', 'AA-RunAll.cmd'
    standardOutput = new ByteArrayOutputStream()
    ext.output = {
   return standardOutput.toString()
 }
 }
   genOCFWUploadFiles.dependsOn build

```

but still nothing happens.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 3:48am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/5 "2013-11-20T03:48:00Z")

</div>

I tried it with \<\< and without it also as in some examples. BTW, what does \<\< mean?

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 11:08am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/6 "2013-11-20T11:08:00Z")

</div>

How is it you can provide input to the process?

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 20, 2013, 1:59pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/7 "2013-11-20T13:59:00Z")

</div>

You can define dependencies between tasks using the method [Task#dependsOn](http://www.gradle.org/docs/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:dependsOn). When [specifying the gradle command in a console/terminal](http://www.gradle.org/docs/current/userguide/gradle_command_line.html), you can list the task(s) that should be executed.

If you just want to execute the single task, you can do so by running “gradle genOCFWUploadFiles”. If you want the task to be executed as a task dependency, it’s a matter of when you want this to happen. For example if you want the task to be executed before the task “build”, you can declare a dependency as such:

```gradle
build.dependsOn genOCFWUploadFiles

```

I don’t understand your last question. Please give us more information on it.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 20, 2013, 2:01pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/8 "2013-11-20T14:01:00Z")

</div>

\<\< means the same as doLast. In your case you do not need this operator as you are configuring a custom task. Please refer to the Gradle documentation for more information.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 3:38pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/9 "2013-11-20T15:38:00Z")

</div>

Thanks for the info.

Say I have to type some text in the console. How do redirect this input to the task?

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 20, 2013, 4:01pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/10 "2013-11-20T16:01:00Z")

</div>

I’d probably use a [project property](http://www.gradle.org/docs/current/userguide/userguide_single.html#sec:gradle_properties_and_system_properties) for that via the -P command line option.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 4:06pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/11 "2013-11-20T16:06:00Z")

</div>

I did not get this. Sometimes you have to press Y, N or any key in the batch file programs. I want to send this to the command.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 20, 2013, 4:09pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/12 "2013-11-20T16:09:00Z")

</div>

You can provide any desired value for your project property from the command line e.g. -PyourProp=yes or -PyourProp=no. Then within your task you use the value of this property to configure the task.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 4:14pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/13 "2013-11-20T16:14:00Z")

</div>

I am not looking to pass this to gradle. Like where I get Stdout like standardOutput = new ByteArrayOutputStream() I want to get the Stdin and perhaps write a string like “YN Y”. I know what I want to pass to the stdin of the process before hand.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 20, 2013, 4:53pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/14 "2013-11-20T16:53:00Z")

</div>

Does this look right:

```gradle
standardInput = new ByteArrayInputStream('YN Y '.getBytes("UTF-8"))

```

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 20, 2013, 10:39pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/15 "2013-11-20T22:39:00Z")

</div>

What are you planning to do with this input? Pass it on to the external cmd file? The Exec task is configured before any task action is executed. So any input you get from a user would (which is done during the execution phase) to be used to configure the Exec task.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 21, 2013, 6:11am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/16 "2013-11-21T06:11:00Z")

</div>

At this moment I do not want user input but just sent some known text to the console.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [November 21, 2013, 1:37pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/17 "2013-11-21T13:37:00Z")

</div>

You can simply add a doFirst action to your task that contains a println with the message you want to display.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 22, 2013, 9:58am UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/18 "2013-11-22T09:58:00Z")

</div>

I changed

```gradle
task genOCFWUploadFiles(type:Exec) {
 workingDir './OCManualBuildFiles'
    commandLine 'cmd', '/c', 'AA-RunAll.cmd'
    standardInput = new ByteArrayInputStream('yn '.getBytes())
    dependsOn 'build'
 mustRunAfter 'build'
 }

```

from

```gradle
task genOCFWUploadFiles(type:Exec) {
 workingDir './OCManualBuildFiles'
    commandLine 'cmd', '/c', 'AA-RunAll.cmd'
    standardInput = new ByteArrayInputStream('yn '.getBytes())
 }
  build.dependsOn genOCFWUploadFiles

```

which worked. The new version does not work. What might be the problem. I am trying to learn how to specify dependencies for Exec tasks.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 22, 2013, 12:31pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/19 "2013-11-22T12:31:00Z")

</div>

Did you notice that you switched the direction of the dependency? ‘build.dependsOn genOCFWUploadFiles’ isn’t the same as ‘genOCFWUploadFiles.dependsOn build’.

---

<div class="post-metadata">

**Author:** ![sirinath](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sirinath/32/76_2.png) [@sirinath](https://discuss.gradle.org/u/sirinath)\
**Post date:** [November 22, 2013, 12:57pm UTC](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589/20 "2013-11-22T12:57:00Z")

</div>

That was a earlier mistake. If I am using depends on in genOCFWUploadFiles(type:Exec, dependsOn: ‘…’) or task genOCFWUploadFiles(type:Exec) {

dependsOn ‘…’

}

such that the task will execute after building what is the exact syntax?

[Next page](https://discuss.gradle.org/t/how-to-run-a-cmd-file-in-directory-relative-to-the-project/7589.md?page=2)
