In this series of posts, we will see various ways to implement multi module, hierarchical / parent-child projects using gradle. To provide a working example, this application implements a simple spring boot based rest like service.
This series:
Maven multi module example
Gradle multi project (module) example – Type I (nested)
Gradle multi project (module) example – Type II (flat)
The application contains 3 modules.
- parent – parent gradle project
- service – service module
- web – web module
Though, the parent project can also contain src directory, in this example, src directory is not present and it just contains gradle files.
Directory structure
sample-gradle-hierarchical-flat-example |---parent |------build.gradle |------settings.gradle |---service |------src |------build.gradle |---web |------src |------build.gradle
As you can see above, the root project directory ‘sample-gradle-hierarchical-flat-example’ contains 3 projects/directories which are ‘parent’, ‘service’ and ‘web’. Though this follows a flat structure physically, the ‘service’ and ‘web’ projects are child projects to ‘parent’ logically.
parent/settings.gradle
rootProject.name = 'parent' includeFlat 'service' includeFlat 'web'
In terms of configurations, the only difference between nested vs flat structure lies in the settings.gradle file where we use includeFlat in case of flat structure and in nested structure, we use include.
Unlike the nested structure, the flat structure helps in terms of source control by providing the capability to create individual repository for each projects.
For more explanations and code, please refer to the first post in this series, Gradle multi module example – Type I (nested).
Build & Test
To build and execute, under parent directory, execute the following command.
gradle bootRun
To test, on browser, point to the following url.
http://localhost:8888/sample/hello
Please refer to gradle documentation to explore more about multi module builds using gradle.
Thanks a lot, it’s very useful. The only thing I see that trying to build the subproject from its folder fails. As far as I understand that’s because the process fails to locate settings.gradle located in the parent folder. Do you know if that is something that can be fixed?
LikeLike
I had same issue as above comment, how can we run the build from inside child project (Service or Web), how do refer the build to parent build file when running from child project? is it possible?
LikeLike