Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle -- execute multiple commands from task

Tags:

gradle

extjs

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?

like image 772
stackato Avatar asked Feb 22 '16 18:02

stackato


People also ask

How do I run multiple tasks in Gradle?

Executing Multiple Tasks You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

How do I run Gradlew clean?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

Where should I run Gradle commands?

Press ⌃⌃ (macOS), or Ctrl+Ctrl (Windows/Linux), type "gradle" followed by the gradle task name or names. We can, of course, run Gradle commands from the terminal window inside IntelliJ IDEA. Open this with ⌥F12 (macOS), or Alt+F12 (Windows/Linux).

How to use string as Task name in Gradle?

You can also use strings for the task names. Take a look at the same hello example. Here we will use String as task. Copy and save the following code into build.gradle file. Execute the following command in the command prompt. It executes the script which is mentioned above. You should execute this, where the build.gradle file stores.

How do I execute a Gradle program from command prompt?

Execute the following command in the command prompt. It executes the script given above. You should execute this, where the build.gradle file stores. You can also use all the properties through the tasks collection. Copy and save the following code into build.gradle file.

How to achieve task dependency using a task object in Gradle?

There is another way to achieve task dependency which is, to define the dependency using a Task object. Let us take the same example of taskY being dependent on taskX, but here, we are using task objects instead of task reference names. Copy and save the following code into build.gradle file. Execute the following command in the command prompt.

How do I skip a task without modifying my Gradle files?

What if we only want to skip a single task without modifying our gradle files? The answer is by using the -x command-line option and providing the name of the task to exclude straight into the command console. And what if we want to skip more than one task?


4 Answers

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

like image 179
Drazul Avatar answered Oct 10 '22 16:10

Drazul


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.

like image 31
Opal Avatar answered Oct 10 '22 17:10

Opal


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'
 }
}
like image 25
panser Avatar answered Oct 10 '22 16:10

panser


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()
       }
    }
like image 45
NickUnuchek Avatar answered Oct 10 '22 16:10

NickUnuchek