43

I have 2 separate apps (in one project) that require 2 separate builds (sencha cmd). I have been asked to create a gradle script that will do the builds for both apps.

I created a task that builds one app, but am having troubles using the same task to build the 2nd app.

This is what I have so far:

task senchaCmdBuild (type: Exec) {
  workingDir 'src/main/app/MYAPP'
  commandLine 'cmd', 'c', 'sencha app build'
}

and this works fine.

When I add the following 2 lines to above task:

 workingDir 'src/main/app/MYOTHERAPP'
 commandLine 'cmd', 'c', 'sencha app build'

the first command is ignored and only the second command executes.

So is there anyway I can execute both commands with one task?

Opal
  • 81,889
  • 28
  • 189
  • 210
stackato
  • 1,085
  • 1
  • 19
  • 38
  • 1
    create two `Exec` tasks and run them both from a master task. – RaGe Feb 22 '16 at 18:27
  • 2
    I feel like there must be a solution where you could leverage `&&` on the commandLine to execute multiple things. Alternatively, you could just invoke a shell script that did both. – Eric Wendelin Feb 22 '16 at 23:15

4 Answers4

88

You can use the second way to declare task types on gradle.

task senchaCmdBuild {
  doLast {
    exec {
      workingDir 'src/main/app/MYAPP'
      commandLine 'cmd', 'c', 'sencha app build'
    }
    exec {
      workingDir 'src/main/app/MYOTHERAPP'
      commandLine 'cmd', 'c', 'sencha app build'
    }
  }
}

You need put the exec method in doLast in order to be executed only on execution flow

Drazul
  • 1,141
  • 1
  • 10
  • 12
  • 5
    If you're doing this inside a plugin, you need to use `project.exec` (as well as `project.task`) to call the proper DSL methods. – Elias Dorneles Feb 26 '17 at 17:08
  • Is there a way to only run the second command if the first one fails? – Noah Andrews Dec 20 '17 at 19:26
  • @NoahAndrews I currently writting different syntax for that, it taken from groovy. With this you can execute something: def result = "echo 1".execute() With this you wait until finish: result.waitFor() and with this you get result value: result.exitValue() – Drazul Dec 21 '17 at 16:00
9

It's impossible to configure (run) multiple commands for the task of type Exec. commandLine it's just a setter - the last one wins. If you need to run multiple commands the best idea is to implement multiple tasks as @RaGe suggested in the comment or to write a custom task and use groovy's native mechanisms - execute method.

Opal
  • 81,889
  • 28
  • 189
  • 210
7

You also can use gradle methods instead create fictive tasks

task senchaBuild() {
 doLast {
    senchaBuild_steps()
 }
}

void senchaBuild_steps() {
 exec {
    workingDir 'src/main/app/MYAPP'
    commandLine 'cmd', 'c', 'sencha app build'
 }
 exec {
    workingDir 'src/main/app/MYOTHERAPP'
    commandLine 'cmd', 'c', 'sencha app build'
 }
}
panser
  • 1,949
  • 22
  • 16
1

Use .execute() at doLast block

task myTask(group: "my-group") {
       doLast {
             println "Starting..."
             println "echo \"MyEcho1\"".execute().text.trim()
             println "echo \"MyEcho2\"".execute().text.trim()
       }
    }
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138