0

I am referring to my post here :Moving Jib configuration of one module into a new module, to refactor multi-module gradle project

With the same refactoring goal of keeping jib related configuration in a separate module, I want to inject some "runtimeOnly" dependencies from the new module so that jar gets added in the classpath.

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

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

but ./gradlew jibDockerBuildWebapp won't add the ":abc" module artifacts (jar) in the war lib directory. Only way I am able to get this working by adding "runtimeOnly project(':abc') in the ":webapp" module's build.gradle file.. but that's not I intend to do. Can you please suggest how to get this working?

I was searching for diff options in Jib settings if I could add the module dependency there for it to add the artifacts in the lib directory. I need to add additional jars to run a setup program before starting tomcat.

Santi
  • 67
  • 7

1 Answers1

1

You can just add dependencies to a module from another module.

// to make sure :webapp is evaluated before this project
evaluationDependsOn(':webapp')
project(':webapp').dependencies {
  runtimeOnly 'junit:junit:4.13'
}

['jib', 'jibDockerBuild', 'jibBuildTar'].each { jibTaskName ->
   ...

However, I'm not sure if this is a good practice. It's the same as defining dependencies in the target build.gradle, and the only difference is that it's being done at a different place. I think this may confuse first-time readers of your repo as to how and why some dependencies are added out of the blue.

BTW, the following also worked for me:

  ...
  task "${jibTaskName}Webapp" {
    doFirst {
      project(':sub1') {
        jib.to.image='jib-sub1'
        dependencies {
          runtimeOnly 'junit:junit:4.13'
        }
      }
    }
  }

However, it's not exactly same as above in that the dependencies are added in a later phase (not in the Gradle configuration phase) and only when the task is executed. Again, I'm not sure if this is a good practice.

Chanseok Oh
  • 3,920
  • 4
  • 23
  • 63
  • Many thanks for your help. Actually I have to run a java application before tomcat startup as the java application creates the needed files for tomcat to use. Ideally I could have added the java application code in the webapp itself but I want to avoid making changes in the webapp codebase (to avoid any side effects at this stage). I realized that all the dependencies are placed in the webapp lib directory and only way I could find adding jars in the image is by adding into the project dependencies.. that's why I am injecting those dependencies from outside. – Santi Sep 28 '21 at 21:59
  • If there is a better way of adding any modules artifacts and its dependencies in a different directory for war project, I would certainly use that. I think what I am trying to do here requires a mix of jar and war actions. – Santi Sep 28 '21 at 21:59
  • Just FYI (not that this is a better way), Jib also has options like [`jib.extraDirectories`](https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#adding-arbitrary-files-to-the-image) and [`jib.configurationName`](https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#extended-usage). – Chanseok Oh Sep 29 '21 at 14:46
  • However, `jib.configurationName` is used via [`project.configurations.`](https://github.com/GoogleContainerTools/jib/blob/6caaac21f5158bae5ac75613bca43f78c28c76e1/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java#L203), so it is always against the project where Jib is configured. – Chanseok Oh Sep 29 '21 at 14:57