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

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

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.

:clean, :build, :buildDependents, :buildNeeded, :eclipse

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?

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

I changed the above to:

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.

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

How is it you can provide input to the process?

You can define dependencies between tasks using the method Task#dependsOn. When specifying the gradle command in a console/terminal, 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:

build.dependsOn genOCFWUploadFiles

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

<< 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.

Thanks for the info.

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

I’d probably use a project property for that via the -P command line option.

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.

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.

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.

Does this look right:

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

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.

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

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

I changed

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

from

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.

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

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?