Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple bash commands in single Gradle Exec task vs. multiple Gradle Exec Tasks each with a single command

Tags:

gradle

I'm using gradle to automate docker publishing and tagging. I currently have the following set of tasks:

task dockerTagLatest(type: Exec) {
    description "Build a Docker image of the current version and tag it as 'latest'"
    dependsOn 'docker'
    group 'Publishing'

    commandLine(['docker', 'tag', dockerImageName, dockerImageLatest])
}

task dockerPush(type: Exec, overwrite: true) {
    description 'Push the Docker image of the current version to the internal Docker hub'
    group 'Publishing'
    mustRunAfter 'dockerTagLatest'

    commandLine 'docker', 'push', dockerImageName
}

task dockerPushLatestTag(type: Exec) {
    description "Push the 'latest' tag to the internal Docker hub"
    group 'Publishing'
    mustRunAfter 'dockerTagLatest'

    commandLine 'docker', 'push', dockerImageLatest
}

task dockerPublish() {
    description "Push the Docker image of the current version and the 'latest' tag to the internal Docker hub"
    dependsOn 'dockerTagLatest'
    dependsOn 'dockerPush'
    dependsOn 'dockerPushLatestTag'
    group 'Publishing'
}

Would it be better to have something like this?

task dockerPublish(type: Exec) {
    commandLine 'bash', '-e', '-c', """
        docker tag ...
        docker push ...
        docker push ...
    """
}

Obviously the second approach is not Windows friendly, but disregarding that for the moment, is it better to have a set of dependent Exec tasks, or to lump all of the command line commands into a single task? I've gotten feedback that the latter is more readable, but I think the first approach is more Gradle-like. Thoughts?

like image 656
Michael Wu Avatar asked Nov 30 '15 22:11

Michael Wu


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.

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

Which is the valid syntax to set default tasks in Gradle?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.


1 Answers

Is there ever a scenario where you wouldn't want to run all of these tasks in that exact order? Could any of them be parallelized by future Gradle features?

If the answer to either of those is "yes", then you may want to use separate tasks; otherwise one task is just fine.

like image 53
Eric Wendelin Avatar answered Oct 20 '22 21:10

Eric Wendelin