Santi
Santi

Reputation: 87

Injecting plugin/runtimeOnly configuration(s) from one module to another module (within the same project) in gradle multi module project

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.

Upvotes: 0

Views: 311

Answers (1)

Chanseok Oh
Chanseok Oh

Reputation: 4306

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.

Upvotes: 1

Related Questions