Skip to content

Moving Jib configuration of one module into a new module, to refactor multi-module gradle project

I have a multi-module gradle project and one of the modules is a webapp. I added Jib configuration within the webapp build.gradle file to generate the container images. Similarly I have a few modules for command-line applications and I use Jib jar config to create container images.

I want to move the Jib specific configuration in new modules to unclutter the webapp/app build.gradle files and want achieve this by creating new modules (say ‘:container:tomcat’, ‘:container:app1’) and setting up a task (or something) to set Jib config here for webapp/app project. I am not sure what gradle apis I need to call to achive the same? I know I can access the webapp/app module from new module build.gradle file but not sure how to set the Jib configuration there.

Can someone please suggest how to achieve this?


Being on Gradle, there are many ways to achieve whatever you want. One idea of mine is to create a new module (say, jibconf) for the following build.gradle.

['jib', 'jibDockerBuild', 'jibBuildTar'].each { jibTaskName ->
  task "${jibTaskName}Webapp" {
    doFirst {
      project(':webapp').jib {
        to.image = 'jib-webapp'
        // and more ....
    finalizedBy ":webapp:$jibTaskName"

  task "${jibTaskName}App" {
    doFirst {
      project(':app').jib {
        to.image = 'jib-app'
        // and more ...
    finalizedBy ":app:$jibTaskName"

Then running ./gradelw jibW or ./gradle jibDBW at the root builds a webapp image, while ./gradlew jibA or ./gradlew jibDBA builds an app image.

Note that it’s recommended to register a task than outright creating it, so I’d actually do this:

  tasks.register("${jibTaskName}Webapp") { ...