Project dependency refers to compiled classes not JAR

I setup a multi-project build. Let’s call my two projects “lib” and “app”. “app” requires services registered through META-INF/services generated from “lib”. It needs to be able to find the JAR generated from “lib” to build up a custom classpath.

Here’s an excerpt of the build.gradle.kts file of “app”.

dependencies {
    compile(project(":lib"))
}

tasks.create<MyTask>("doSomething") {
    addCompileClasspath = true
}.dependsOn(":lib:assemble")

tasks["build"].finalizedBy(tasks["doSomething"])

In a Gradle plugin I’m also writing, I need to get the Java compile classpath. I get it from within my plugin like this (Java code):

project.getConfigurations().getByName("compileClasspath")

When I dump this out as a classpath, it points to only the compiled classes and not the generated JAR:

/Users/blah/lib/build/classes/java/main

This directory does not contain resources or the relevant META-INF/services file, so my services aren’t found when executing “app”.

Why am I getting the compiled classes and not the JAR?

More details if needed:

My settings.gradle.kts looks like this:

rootProject.name = "foo"
include(":lib")
include(":app")

Then root build.gradle.kts is pretty minimal. Looks like this:

allprojects {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

Note that “lib” applies the java-library plugin while app does not.

When I dump this out as a classpath, it points to only the compiled classes

This is desired/expected behavior

tasks[“build”].finalizedBy(tasks[“doSomething”])

Attaching to the “build” task seems a bit late in the lifecycle. Have you considered using the “classes” or “processResources” tasks instead?

project.getConfigurations().getByName(“compileClasspath”)

Try printing sourceSets.main.output or sourceSets.main.runtimeClasspath these will include the classes directory and the processed resource directory.

If it were me I’d attach the logic to the processResources task

Eg:

apply plugin: 'java-library' 
task generateResources {
   dependsOn compileJava 
   outputs.dir "$buildDir/generated/resources" 
   doLast {...} 
} 
processResources {
   from generateResources
}