Tilt: Request: example for java based service using Gradle

Created on 20 Dec 2019  路  4Comments  路  Source: tilt-dev/tilt

Hi there, is there any plan to come up with some examples for java based services which use Gradle as build tool? Think it will be cool to see that example to demonstrate the multi-language support of Tilt.

Things I am interested: Java hot reloading, build time latency, remove debugging, Interrop with Gradle

Thanks!

enhancement

All 4 comments

Hi Leo. Yes, there is!

We're working on an extensions system right now that'll make it really easy to create and share functionality, and Java comes up often enough that we'll definitely have some extensions for it.

@qibinlou Thanks for the suggestion!

If I was going to try it, the first thing I would try is to play around with Jib and the Jib/Gradle plugin for building java container images. https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin

And then use Tilt's custom_build to invoke jib (https://docs.tilt.dev/custom_build.html).

But you're right that we should come up with a working example for demoing what this looks like in action

I have just setup my gradle project with Tilt, and I'm putting here in case it helps anyone:

For starters, mine is a Spring Boot application, so configured this gradle plugin (instead of jib) with:

docker {
    springBootApplication {
        images = ["${project.group}/${project.name}:${project.version}"]

        if (project.hasProperty("dockerRef"))
            images.add(project.findProperty("dockerRef"))
    }
}

The dockerRef is introduced as a property so that we can use one-time tags.

Then, as for the Tiltfile:

custom_build(
    "project.group/project.name",

    # Execute the task exposed by the gradle plugin
    #  It is important to use the gradle wrapper, instead of your global 
    #  installation, so that gradle caches are properly re-used.
    "./gradlew dockerBuildImage -PdockerRef=$EXPECTED_REF",

    # The files to trigger update.
    #  Note: you might also want to add './build/classes/java/main'
    #   
    #  [^1]
    deps = [
        "./build/classes/kotlin/main",
        "./build/resources",
        "./build.gradle"
    ],

    # One-to-one mapping on 'deps'
    #  The 2nd argument of the 'sync' function is heavily dependent on the
    #  way your image is built. For my case, the Dockerfile generated 
    #  (seen in `./build/docker/Dockerfile`) by plugin  I am using is as simple as:
    #    ...
    #    WORKDIR /app
    #    COPY libs libs/
    #    COPY resources resources/
    #    COPY classes classes/
    #  
    #  Hence, the destination `/app/*`
    # 
    #  [^2]
    live_update=[
      sync("./build/classes/kotlin/main", "/app/classes"),
      sync("./build/resources", "/app/resources"),
      restart_container()
    ]
)

The downside to this setup, is that you need to explicitly tell your IDE to build your sources, so that the changes is written on build/, and ultimately triggering Tilt's reload. For instance, in IntelliJ I do Build > Recompile or Build > Build Project.


We have a full Java tutorial now! https://docs.tilt.dev/example_java.html

Was this page helpful?
0 / 5 - 0 ratings