简体   繁体   中英

Adding subproject resolved dependencies to earlib in gradle 5.5.1

I have a multi-project build with 100+ EAR files. The subprojects more or less use a convention like this:

subproject1
 -| subproject1JAR
 -| subproject1EAR
 -| subproject1WAR

subproject2
 -| subproject2JAR
 -| subproject2EAR
 -| subproject2WAR

When I refer to JAR, below, I'm referring to the business logic/SDK contained in, eg, subproject1.jar .

I have some logic in an afterEvaluate block in subprojects{} that parses the .classpath files of the subprojects in order to build the EAR files. This is working but one thing I'm not getting as compared to the legacy (manual) builds is that the EAR/lib folder needs to contain all the dependencies of the JAR file.

Apologies for the wall of code here, I'm just trying to provide the full context for this question.

// this is from subprojects
if(proj.projectDir.name.endsWith('EAR')){ <-- configures EAR proj based on folder name
  proj.apply plugin: 'ear'

  proj.ear {
    baseName = proj.projectDir.name
    // snip other ear config stuff
  }

  if(f_ui.exists()){ // <-- f_ui is a File pointed at what ought to be the WAR subproject
    proj.dependencies{
      deploy project(path:":${proj_war}WAR", configuration:'archives')
    }
  }

  if(f_jar.exists()){ // <-- f_jar is a File pointed at what ought to be the JAR subproject
    proj.dependencies{
      earlib project(path:":$proj_jar", configuration:'archives')  
    }
    def jar_proj = project(":$proj_jar")
    proj.evaluationDependsOn(":$proj_jar")
    jar_proj.configurations.runtime.allDependencies.forEach { <-- trying to add deps here but
      println "##> DEPENDENCY $it"                            <-- these are empty here
      proj.dependencies.add('earlib', it)
    }
  }
  // ## END OF EAR PROJECT CONFIGURATION
}

You can see above where I'm trying to add the dependencies OF THE JAR to the earlib configuration but at this point it seems the dependencies have not been resolved so no dependencies are added.

I believe this unanswered question seems to be asking the same thing.

Any suggestions on how I should be doing this in order to get the dependencies of the JAR into the ear/lib folder?

I certainly don't think this is the best solution but it seems to be working so I'll describe what I did.

Create a custom configuration into which all the dependencies I needed to show up in the earlib were added:

subprojects { Project proj ->
  def projname = proj.name
  afterEvaluate { 
    logger.info "## {} afterevaluation #0 started",projname

    ["Proj1","proj2", "proj3"].each{ depson ->
       if (classpathFileContains(proj,depson)) {
         proj.configurations.maybeCreate('extra')
         proj.dependencies.add('extra', project(":${depson}"))
       }
    }
 ...

Add those as implementation dependencies:

try {
  // if the project has 'extra', add them to the implementation
  proj.configurations.named('extra')
  proj.configurations.extra.dependencies.each{
    proj.dependencies.add('implementation', it)
  }
} catch(e) {
  logger.info "## Project {} did not have an extra", proj.name
}

Add the jar file and then add the extra dependencies from the custom configuration:

proj.dependencies{
  earlib project(":$proj_jar")
}

try {
  jar_proj.configurations.named('extra')
  jar_proj.configurations.extra.dependencies.each{
    proj.dependencies.add('earlib', it.copy())
  }
...

Incidentally, I ran across several other instances of people having this challenge with earlib (see ex1 , ex2 , ex3 , plus the reference in the question).

I'm going to try to simplify things and will update this answer if I have an aha moment but as of now this approach is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM