Share generated openapi file between projects

I recently needed to do something similar, but in my case I was sharing compiled test classes with other subprojects that rerun the same tests with different dependency versions. It looks like what you don’t have correct is the configuration of the consumer so that it correctly depends on the production of the shared artifact(s):

In the producer subproject:

val sharedTestClasses: Configuration by configurations.creating {
    isCanBeConsumed = true
    isCanBeResolved = false
}

val testClasses: Task by tasks.getting

val sharedTestClassesJar by tasks.registering(Jar::class) {
    dependsOn(testClasses)
    archiveClassifier.set("tests")
    from(sourceSets.test.get().output)
}

artifacts {
    add(sharedTestClasses.name, sharedTestClassesJar)
}

In consumer subproject:

val externalTestClasses: Configuration by configurations.creating {
    isCanBeConsumed = false
    isCanBeResolved = true
}

dependencies {
    externalTestClasses(
        project(mapOf("path" to ":producer-project-name", "configuration" to "sharedTestClasses"))
    )
}

You will not have a Jar task, obviously, since your artifact(s) will be file(s), but I included the producer and consumer for completeness. For me, with Gradle 7.3, this correctly makes the consumer project depend on the producer task that creates the items to be shared.