How to bundle static resources from other subproject into java application

Hi, a common application pattern is to define a java backend subproject, and an SPA frontend project driven mainly by npm/yarn. The frontend files could be packaged as a webjar and served from the backend as static files.

How should such 2 gradle backend and frontend subprojects be defined and linked to each other cleanly?

Both

provide different ways, neither of which seems idiomatic gradle, or anyway I cannot tell which one is clean (with respect to build isolation, incremental builds, etc.)

And when I tried to copy the first, I got:

 > No matching variant of project :frontend was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'apiElements' capability kotlin-spring-boot:frontend:unspecified declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 14 and the consumer needed a runtime of a component compatible with Java 11
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')

The general pattern is that base subproject A has a task that creates a jar, and java subproject B wants to include that jar in it’s classpath at runtime.

This answer is a start, but not very verbose: Multiproject configurations

I could not find documentation of how to achieve that in the gradle docs.

Solution from here:

In the “frontend” subproject create a jar from some static files, and make it an artifact belonging to a configuration:

tasks.register('packageDistribution', Jar) {
  // ... add files here
  // for webjar, use next line
  // into "META-INF/resources"
}

// define new configuration
configurations {
    webjar
}

// tell gradle the task adds to the new configuration
artifacts {
    webjar packageDistribution
}

In the java subproject, do:

runtimeOnly(project(path = ":frontend", configuration = "webjar"))

Anyone, please correct me if that’s wrong.

I believe this question is very close to what you are trying to accomplish. Please see the answer I gave there:

Also, see this documentation:

https://docs.gradle.org/current/userguide/cross_project_publications.html#cross_project_publications