javaspring-bootgradleservice

How to refer code from one independent gradle project to another gradle project?


enter image description here

Project 
  |- api-gateway  (package)
  |- base-service (package)
  |- service-discovery (package)
  |- user-service (package)

This is my project structure.

There is no root project. Every project is independent. base-service has UserModel class. I want to use that UserModel class inside the user-service. I want to avoid duplicate code so I kept all model classes in base-service. When we build user-service it should refer to the base-service code and include that also in the JAR. How to achieve it?

One way I got from the internet is by mentioning like this:

includeBuild(../base-service)

in settings.gradle of user-service and product-service. But still, I am not able to use any class from base-service.


Solution

  • In the root directory of your build, you should add a settings.gradle file, where you declare all your (sub)projects in your build. This would look like this with your structure:

    include 'api-gateway'
    include 'base-service'
    include 'service-discovery'
    include 'user-service'
    

    You need now in every subproject a 'build.gradle' file (I assume you have already one), where you can declare the dependencies inside your build, e.g. the build.gradle file in your user-service directory contains

    ...
    
    dependencies {
      implementation project(':base-service')
    
      // other dependecies
      
    }
    
    ...
    

    This will make the classes from base-service available in user-service.