How to configure bootBuildImage task to be up-to-date when no changes to source code

1.7k Views Asked by At

I'm trying to use spring boot 2.3's new support for creating docker images, but the bootBuildImage gradle task is never up-to-date. This unfortunately causes a new docker image to be generated even if no source code was changed.

My goal is to have a static build command that doesn't result in new images being produced unnecessarily. So something like one of the two scenarios below:

./gradlew bootBuildImage (but does nothing if no source code has changed)

OR

./gradlew someOtherTask (if this task is not up-to-date, it triggers bootBuildImage)

My latest effort was to configure bootBuildImage to only run if the bootJar task is not up to date:

tasks {
    val bootJarTask: TaskProvider<BootJar> = this.bootJar
    bootBuildImage {
        outputs.upToDateWhen {
            bootJarTask.get().state.upToDate
        }
    }
}

But this fails with this error (for some reason this particular task hates jars as inputs)

> Unable to store input properties for task ':bootBuildImage'. Property 'jar' with value '/demo/build/libs/demo-0.0.1-SNAPSHOT.jar' cannot be serialized.

Surely I'm missing something obvious here! The reason I need bootBuildImage to only produce an image when necessary is because I've got a multi-project build. I don't want subprojects to generate and push a new image even when nothing in them changed.

Using Spring Boot 2.3.4, Gradle 6.6.1, Java 11.

1

There are 1 best solutions below

0
On

This seems to work:

    val bootJarTask: TaskProvider<BootJar> = this.bootJar
    
    bootBuildImage {
        onlyIf {
            !bootJarTask.get().state.skipped
        }
    }