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


(sirinath1978m) #1

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

(Benjamin Muschko) #2

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?


(sirinath1978m) #3

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


(sirinath1978m) #4

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.


(sirinath1978m) #5

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


(sirinath1978m) #6

How is it you can provide input to the process?


(Benjamin Muschko) #7

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.


(Benjamin Muschko) #8

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


(sirinath1978m) #9

Thanks for the info.

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


(Benjamin Muschko) #10

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


(sirinath1978m) #11

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.


(Benjamin Muschko) #12

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.


(sirinath1978m) #13

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.


(sirinath1978m) #14

Does this look right:

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

(Benjamin Muschko) #15

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.


(sirinath1978m) #16

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


(Benjamin Muschko) #17

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


(sirinath1978m) #18

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.


(Peter Niederwieser) #19

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


(sirinath1978m) #20

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?