Search code examples
javagradleclassloader

How is the classpath setup in a multimodule gradle project during runtime?


I'm quite new to gradle and especially multimodule gradle projects, so I have a small example project created with the following project structure

Main

*src
*tests
*build.gradle
*settings.gradle

*Subproject1
    +src
    +tests
    +build.gradle

So when I tried to load the classes in Subproject1 from a class in the main (root) project, it can not find the class, I would have thought the root project classpath contains the subprojects classes as well. What am I doing wrong here or any material I should go through to understand this?

current settings.gradle file,

rootProject.name = 'main'
include 'Subproject1'


Solution

  • You should take some time to read documentation HERE which explains the concept of multiproject builds and provides some examples on how to create dependencies between sub-projects.

    The root project will not inherit classpath from the subprojects, you will have to declare explicitelly these dependencies as follows

    build.gradle (root project)

    dependencies {
        implementation project(':Subproject1')
    }