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?
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.
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.
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).
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.
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.
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.
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?
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
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.
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'
}
}
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With