单个Gradle Exec任务中的多个bash命令与多个Gradle Exec任务,每个命令都有一个命令

时间:2015-11-30 22:45:16

标签: gradle

我正在使用gradle来自动化docker发布和标记。我目前有以下任务:

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'
}

拥有这样的东西会更好吗?

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

显然,第二种方法不是Windows友好的,但是暂时忽略这一点,是否更好地拥有一组依赖的Exec任务,或者将所有命令行命令集中到一个任务中?我得到的反馈是后者更具可读性,但我认为第一种方法更像是Gradle。想法?

1 个答案:

答案 0 :(得分:3)

是否存在您不希望按照确切顺序运行所有这些任务的情况?他们中的任何一个可以通过未来的Gradle功能进行并行化吗?

如果其中任何一个的答案为“是”,那么您可能想要使用单独的任务;否则一项任务就好了。

相关问题