Hello,
In the example build.gradle file below I’m struggling to understand why project app cannot access the JAR file produced by a dependent project lib & its dependencies:
allprojects {
repositories {
mavenCentral()
}
}
// -----------------------------------------------------------------
project("lib") {
apply plugin: 'java-library'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
api('org.springframework.boot:spring-boot:3.0.0')
}
}
// -----------------------------------------------------------------
project('app') {
apply plugin: 'java'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
implementation(project(':lib'))
}
task copyDependencies() {
doFirst {
copy {
from(configurations.runtimeClasspath)
into(file("$buildDir/dependencies"))
}
}
}
}
If I run the command ./gradlew :app:copyDependencies --info it does not trigger the JAR task to run for the lib project & I can see that the copyDependencies task logs that the lib.jar was not found:
... snip ...
> Task :app:copyDependencies
Caching disabled for task ':app:copyDependencies' because:
Build cache is disabled
Task ':app:copyDependencies' is not up-to-date because:
Task has not declared any outputs despite executing actions.
→ file or directory '/home/g-dx/Workspaces/gradle-issue/lib/build/libs/lib.jar', not found
:app:copyDependencies (Thread[included builds,5,main]) completed. Took 0.002 secs.
My first thought was to add the jar task output to the default configuration of the lib project like this:
artifacts {
"default" jar
}
However, this makes no difference. The lib:jar task is never executed as part of calling app:copyDependencies.
I am aware of simple artifact sharing and the lib project dependency could be expressed as:
dependencies {
implementation(project(path: ':lib', configuration: 'archives'))
}
This does trigger the lib:jar task to run, however doing this I lose access the lib project dependencies required to actually use the library, in this example spring-boot.
The only way I’ve found to make this work is to specify the dependency twice:
dependencies {
implementation(project(':lib'))
implementation(project(path: ':lib', configuration: 'archives'))
}
But this just seems wrong.
What am I missing here? Is it not possible to add the output JAR to the default configuration?