Setting earlib to configurations.runtimeClasspath

Our gradle build for an EAR looks like this:

plugins {
  id 'com.github.jk1.dependency-license-report' version '2.0'
}
apply plugin: 'java-library' // add java and jar tasks
apply plugin: 'war' // add war archive task
apply plugin: 'ear' // add ear archive task
apply plugin: 'eclipse' // build the eclipse classpath

group 'my'
version = findProperty( 'buildVersion') ?: '14.0'
java {
  sourceCompatibility = JavaVersion.VERSION_17
  targetCompatibility = JavaVersion.VERSION_17
}

//optionally provide a static name for the archive, else stick with the convension
def myArchiveName =  findProperty( 'earFile')  ?:  "${group}-${version}.ear"
// some more defines...

sourceSets { // define path for main source set
    main {
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
    }
}

configurations {
  compileOnly.transitive = false // provided by server or product
  compileClasspath.transitive = false
  warweb // zip artifacts for web overlay
  ecj // for eclipse compiler dependency
}

dependencies {
  compileOnly( 
  // provided by server
  )
  // additional compile+runtime jars
   // my artifacts added as overlay of war
  warweb "my:my-core-web:$version" changing true
  warweb "my:my-extra-web:$version" changing true
  deploy fileTree( dir: "$buildDir/libs/", include: '*.war') // add war to ear
  //FIXME how to solve in gradle 8+?
  earlib configurations.runtimeClasspath // add runtime dependencies to ear/lib dir
  ecj 'org.eclipse.jdt:ecj:3.32.0'
}

//build custom jar
jar {
  archiveBaseName = "my-$custom"
  into 'META-INF', { from 'conf/jar' } // add custom deployment descriptors if any
}

war { // war with product and custom artifacts
  dependsOn jar
  from configurations.warweb.files.collect { zipTree( it) } // overlay product web content (html/js) from zips
  archiveFileName = 'web.war'
  classpath = jar // add custom jar output (if any) to WEB-INF/lib
  //add my-* product jars to war classpath
  classpath += configurations.runtimeClasspath.filter { it.name.startsWith( 'my-') }
}

ear { // ear with product sources and libs
  dependsOn war, versionTxt
  archiveFileName = "${myArchiveName}"
  deploymentDescriptor { webModule( 'web.war', 'my') }
  into 'META-INF', { from "$productDir/conf/ear" }
  exclude 'my/**' // else my sources are included in ear root
  rootSpec.exclude 'my-*.jar' // else my jars are added again to ear/lib
}

So we build a jar, then include jar and the related product dependencies (additional jars) in the war.

Additional (non-product) dependencies are then put in the earlib via earlib configurations.runtimeClasspath

Trying to run the build with Gradle 8+, we are seeing:

* Where:
Build file '/code/product/parent/build.gradle' line: 98

* What went wrong:
A problem occurred evaluating project ':parent'.
> Adding a Configuration as a dependency is no longer allowed as of Gradle 8.0.

I understand that configuration is no longer allowed, but I am not sure how to configure earlib to inlcude my dependencies in a similar way?

Update: I think using:

earlib configurations.runtimeClasspath.allDependencies

does what I want, although I don’t grok why that works but

earlib configurations.runtimeClasspath does not…

If you want all that is in runtimeClasspath also in earlib, then the correct way is to make earlib.extendsFrom(runtimeClasspath).

Thank you for replying, I tried this and then I get

Could not get unknown property ‘earlib’ for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

As I mentioned in my previous comment, the approach using
earlib configurations.runtimeClasspath.allDependencies

achieves my goal and resolves the warnings blocking the migration to 8+

Not in the dependencies block. You are configuring configurations there. So do it in the configurations block, or like configurations.earlib.extendsFrom(configurations.runtimeClasspath) top-level if you prefer.

achieves my goal and resolves the warnings blocking the migration to 8+

Sure, whatever works for you.
If your goal was to resolve the warnings, that’s fine.
If you want it to work properly, then not. For example you will not get transitive dependencies, but only directly declared ones. And I’m not sure whether the result is live or a snapshot at the time you call it.

1 Like

@Vampire thank you, that cleared it up! I used

earlib.extendsFrom runtimeClasspath

and it works as described.

1 Like