Make a project depend on the output of another project in a multi project build

I have two projects in a multi project build. One looks like this:

// project-a build.gradle

task generateJar(type: Exec) {
   commandLine "command", "to", "generate", "jar"
}

task generateArtifact(type: Zip, dependsOn: generateJar) {
    outputs.file("/path/to/generated/jar")
}

artifacts { archives generateArtifact }

And the other simply depends on it like so:

// project-b build.gradle

dependencies {
    implementation project(':project-a')
}

My expectation is that when I run a build on project b, it will first try to run generateArtifact + generateJar from project a, then get the outputted jar for use in project b, however I just get a “package does not exist” error in :project-b:compileJava. Based on the output, it looks like it is not trying to run any tasks in project-a, almost like it doesn’t recognize the dependency exists

This statement

dependencies {
    implementation project(':project-a')
}

Says to depend on the “default” configuration of “project-a” but you are putting the artifact in the “archives” configuration. I believe you’ll want to do something like

dependencies {
    implementation project(path:':project-a', configuration:'archives')
}

See https://stackoverflow.com/questions/19936915/gradle-what-is-the-default-configuration-and-how-do-i-change-it

Thank Lance. My confusion was that project-b here is using the java plugin (which I think changes the default to “archives”?) but project-a is not.

which I think changes the default to “archives”

No, I think “default” extendsFrom the “runtime” configuration in a java project. It points to the class files rather than the jar file in the “archives” configuration. So it’s possible to compile dependent projects (and execute tests) without building jars