springspring-bootgradleintellij-ideagradle-multi-project-build

How to create a multi-build gradle project with interdependent spring boot projects and jars


Background

I wrote a spring boot project using gradle and it works fine. I would like to split up the project into libraries (jars) so that I can reuse them.

These are the requirements

  1. Each of the projects are in sibling folders, each containing its own .git repo.
  2. The multi-build project should be in sibling folder, not in the folder that contains the child projects. It also contains its own .git repo.
  3. Projects that are used as libraries only (jar) have tests annotated with @SpringBootTest and these should be runnable independently.
  4. Projects that are used as class libraries should be provided to other projects as "thin jars" without any dependencies included. The using project needs to add whatever dependencies are accumulated by all the projects it uses.
  5. It should be possible to build each child project independently. For projects that depend on other projects in the suite, this means that building the project will use the output of the other projects (jar) that it needs, and if that output is not ready, gradle will first build those projects.
  6. It should be possible to build all projects from the mult-project parent (Dependencies will be worked out and only necessary builds will occur).
  7. Building the entire suite with the multi-project will create a deployable folder containing the parent boot jar and all its dependencies.
  8. I would like to be able to author the projects, edit and debug all of the above with Intellij.

Minimal, Reproducible Example

I have uploaded a minimal, reproducible example (reprex) here: https://github.com/david-sackstein/multiproject
It contains three folders:

  1. productcommon is a jar with a public class ProductFactory.
  2. productservice is a SpringBoot application with a single @RestController that invokes a public method of ProductFactory in one of its methods. productservice has a project dependcy on productcommon.
  3. multiproject is a gradle project with no code that depends on productcommon and productservice
  4. I setup the build.gradle and settings.gradle in all three projects accordingly.

When I build productservice it fails - evidently because the project import of productcommon fails.

What is missing or incorrect in this example?


Solution

  • Today I came across this excellent post on the subject: https://reflectoring.io/spring-boot-gradle-multi-module by Tom Hombergs

    Tom also provides a great example at: https://github.com/thombergs/buckpal

    I may have some follow on questions on the subject after working with his architecture but for the moment, I am happy with this solution.