question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gradle: Support lazy configuration of properties, specifically jib.to.image.

See original GitHub issue

Today the jib.to.image is configured statically in a Gradle task or via cli arguments. This only works in very simple scenarios where Gradle is really only build/pushing the image or using static versions that are known before Gradle is invoked.

In my case, and especially important for local reproducible workflows, Gradle is building up all the JARs etc that are going into the image and versioning the image with a hash of that contents (so re-builds and re-deploys without changes are no-ops). This means the image version isn’t known ahead of time and should be calculated from the inputs, the idiomatic Gradle way to do that is to use a Provider<> i believe https://docs.gradle.org/current/userguide/lazy_configuration.html instead of a normal String.

Currently to work around this I have to do:

jib {
...
}

jibDockerBuild.doFirst {
    jib.to.setImage(jib.to.getImage() + ":version-blahblah")
}

which isn’t ideal. I’d like to be able to configure it this, which lazy configuration approach above supports I believe, like:

jib {
  ...
  to {
     ...
     image = { -> "image:${calculateImageHash()}" }
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nhoughtocommented, Nov 4, 2021

Agree the workaround is quite simple, not very idiomatic Gradle to reach in and mess with task props in custom actions though it has other knock-on effects on up-to-date checking, configuration caching etc. The examples aren’t super clear, bit frustrating, to explain more:

abstract class Greeting extends DefaultTask {
    // A configurable greeting
    @Input
    abstract Property<String> getGreeting()

    // Read-only property calculated from the greeting
    @Internal
    final Provider<String> message = greeting.map { it + ' from Gradle' }

    @TaskAction
    void printMessage() {
        logger.quiet(message.get())
    }
}

tasks.register("greeting", Greeting) {
    // Configure the greeting
    greeting.set('Hi')
    greeting = 'Hi' // Alternative notation to calling Property.set() - only available in Groovy DSL
}

By just wrapping the task variable in a Property<String> where it used to be String you easily support my dynamic setting of that value by users of your gradle tasks, groovy handles the translation, so you can set it normally just a static string:

greeting = 'Hi'

or you can give it a closure instead to pass a more dynamic value:

greeting = provider({ -> 'something more dynamic' })

Just by changing String -> Property<String> and then accessing it via .get() instead.

1reaction
suztomocommented, Nov 4, 2021

In the document (https://docs.gradle.org/current/userguide/lazy_configuration.html), I found cases where task implementer can lazily reference properties. But I didn’t find task users passing a lambda to a task. Do you have a specific section I should read?

(My first impression is that your workaround is more simpler than going through https://docs.gradle.org/current/userguide/lazy_configuration.html)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy Configuration - Gradle User Manual
Gradle provides lazy properties, which delay the calculation of a property's value until it's actually required. These provide three main benefits to build ......
Read more >
google/jib - Gitter
Added lazy evaluation for jib.to.image and jib.to.tags using Gradle Property and Provider. (GoogleContainerTools/jib#2727); (Incubating) Configure multiple ...
Read more >
Create images using Jib Gradle plugin
Create an application · 1. In jib extension we configure the three important fields. The `from`. Configures the base image to build our ......
Read more >
Java Image Build, Part 1 - Gunther Rotsch
Images build with jib can be directly pushed to a container registry. jib comes with a Maven plugin, we're going to use in...
Read more >
Jib vs. Spring Boot for building Docker images - Tom Gregory
Apply the Jib Gradle plugin in a project's build.gradle file like so. plugins {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found