如何在某些情况下跳过gradle任务,由bmuschko / gradle-docker-plugin引入?

时间:2016-04-29 07:35:45

标签: java gradle docker

我使用gradle插件bmuschko/gradle-docker-plugin将docker与gradle集成。我在build.gradle文件中创建了一些任务:

    task copyJar(type: Copy) {
    dependsOn build

    from project.file("$project.buildDir/libs/" << jarName)
    into project.file("$project.buildDir/docker")
}

task createDockerfile(type: Dockerfile) {
    dependsOn copyJar

    // Generate Dockerfile
}

task buildDockerImage(type: DockerBuildImage) {
    dependsOn createDockerfile

    inputDir = createDockerfile.destFile.parentFile
    tag = 'example.com/' << jar.baseName
    if (project.hasProperty('imageTag')) {
        tag += ':' << imageTag
    }
}
task pushDockerImage(type: DockerPushImage) {
    dependsOn buildDockerImage
    conventionMapping.imageName = { 'example.com/' + jar.baseName }
    if (project.hasProperty('imageTag')) {
        conventionMapping.tag = { imageTag }
    }
}

我想要的是当我运行gradle pushDockerImage时,如果源代码没有变化,则可以跳过此任务。但是,现在每次运行gradle pushDockerImage时,它都会执行并持续很长时间将图像推送到docker注册表。

我还运行gradle --info pushDockerImage两次,第二次gradle显示以下信息:

Skipping task ':core/sample:buildDockerImage' as task onlyIf is false.
:core/sample:buildDockerImage SKIPPED
:core/sample:buildDockerImage (Thread[main,5,main]) completed. Took     1.765 secs.
:core/sample:pushDockerImage (Thread[main,5,main]) started.
:core/sample:pushDockerImage
Executing task ':core/sample:pushDockerImage' (up-to-date check took 0.001 secs) due to:
    Task has not declared any outputs.

所以此处buildDockerImage被跳过,但pushDockerImage不是。我怎么能跳过它?

1 个答案:

答案 0 :(得分:0)

我认为你需要在任务copyJar上声明创建的图像作为pushDockerImage任务的输入。 类似的东西:

task pushDockerImage(type: DockerPushImage) {
    dependsOn buildDockerImage
    inputs.file project.file("$project.buildDir/docker")
    ...

有关详细信息,请参阅有关gradle用户指南的第17.9节:https://docs.gradle.org/current/userguide/more_about_tasks.html