How to access only one class from different module in multi-module spring boot application gradle?

Viewed 491

I have 2 modules (Module A and Module B) in my multi-module spring boot application, build using gradle.

Now the Main class for spring boot module is present in Module A. Now I want to access this Main class in Module B.

In Module B, I want to write integration test cases and over a Test case class, I want to mention Main class in SpringBootTest annotation. Something like this:

@SpringBootTest(classes = Main.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class TestController {

}

But here I am unable to find the Main class. What changes should I need to make in gradle file for Module B to support this?

1 Answers

It is best to split your project into modules. Your settings.gradle and build.gradle should look something like this:

settings.gradle

include 'moduleA'
include 'moduleB'

moduleB/build.gradle

dependencies{
  implementation project(':moduleA')
}

More info: https://docs.gradle.org/current/userguide/multi_project_builds.html

If you really want to only have the class as the source file, then this should work:

sourceSets {
    main {
        java {
            srcDirs 'path/to/the/class'
        }
    }
}
Related