# How to bundle static resources from other subproject into java application

**URL:** <https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076>\
**Category:** Help/Discuss\
**Created:** [January 30, 2022, 10:05am UTC](https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076 "2022-01-30T10:05:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thibault\_Kruse](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/thibault_kruse/32/102_2.png) [@Thibault\_Kruse](https://discuss.gradle.org/u/Thibault_Kruse)\
**Post date:** [January 30, 2022, 10:05am UTC](https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076/1 "2022-01-30T10:05:43Z")

</div>

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

- [GitHub - coditory/gradle-webjar-plugin-sample: Sample usage of gradle-webjar-plugin](https://github.com/coditory/gradle-webjar-plugin-sample)
- [frontend-gradle-plugin/examples/multi-projects-war-application at master · siouan/frontend-gradle-plugin · GitHub](https://github.com/siouan/frontend-gradle-plugin/tree/master/examples/multi-projects-war-application)
- [GitHub - TheKojuEffect/spring-boot-angular-gradle-multi-project: Spring Boot Angular Gradle Multi Project Configuration](https://github.com/TheKojuEffect/spring-boot-angular-gradle-multi-project)
- [A Minimalistic Guide to Building and Deploying Monolithic Spring Boot React Applications | by Shekhar Gulati | Xebia Engineering Blog | Medium](https://medium.com/xebia-engineering/a-minimalistic-guide-to-building-and-deploying-monolithic-spring-boot-react-applications-39440035b27)
- [How to integrate output of non-java task to a java project](https://discuss.gradle.org/t/how-to-integrate-output-of-non-java-task-to-a-java-project/25445)

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:

```gradle
 > 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](https://discuss.gradle.org/t/multiproject-configurations/18969)

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

---

<div class="post-metadata">

**Author:** ![Thibault\_Kruse](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/thibault_kruse/32/102_2.png) [@Thibault\_Kruse](https://discuss.gradle.org/u/Thibault_Kruse)\
**Post date:** [January 30, 2022, 11:05am UTC](https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076/2 "2022-01-30T11:05:22Z")

</div>

Solution from here:

> [@How to integrate output of non-java task to a java project](https://discuss.gradle.org/t/how-to-integrate-output-of-non-java-task-to-a-java-project/25445):
>
> I’m trying to integrate output of the Angular 5’s compiler into a java servlet (war) as a package containing resource files - autogenerated generated html, js, styles etc. After some struggling I’ve got this build script. The top level project is ‘topaz’, angular subproject is ‘topaz-angular’. It works, but for some reason the resulting war has two copes of scripts - one within topaz.war/WEB-INF/lib/topaz-angular.jar and another one is in topaz.war/static, not packed. Another minor defect is th…

In the “frontend” subproject [create a jar](https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_archives_example) from some static files, and [make it an artifact](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.ArtifactHandler.html) belonging to a configuration:

```gradle
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:

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

```

Anyone, please correct me if that’s wrong.

---

<div class="post-metadata">

**Author:** ![EarthCitizen](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EarthCitizen](https://discuss.gradle.org/u/EarthCitizen)\
**Post date:** [February 1, 2022, 4:10am UTC](https://discuss.gradle.org/t/how-to-bundle-static-resources-from-other-subproject-into-java-application/42076/3 "2022-02-01T04:10:44Z")

</div>

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

> [@Share generated openapi file between projects](https://discuss.gradle.org/t/share-generated-openapi-file-between-projects/41868/5):
>
> 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 testClass…

Also, see this documentation:

[https://docs.gradle.org/current/userguide/cross\_project\_publications.html#cross\_project\_publications](https://docs.gradle.org/current/userguide/cross_project_publications.html#cross_project_publications)
