Working with Gradle Modules
Since my adventures with Java began, I have always used Maven as a build and dependency management tool. I had an awareness and even used gradle as a consumer, but never used it for anything but simple use-cases. Whilst building an example application the need arose to migrate a simple gradle app to a multi-module build.
Why use Gradle Modules? #
Moving to a modular design helps to keep your code cleaner. Whilst it can seem simpler to maintain a monolithic project, over time changing requirements and developers often lead to a tangle developing within a project. Modules can help maintain clear boundaries within a project, and to aid better understand the intentions of the design. Not only that, your dependencies become explicit rather than implicit.
Bootstraping Gradle Modules #
Declare dependency on sibling project #
// project/example-app/settings.gradle
rootProject.name = 'example-app'
import(':example-library')
When declaring the dependency on the library, we need to ensure that the library is compiled before attempting to compile our application. We declare this requirement using evaluationDependsOn
.
// project/example-app/build.gradle
evaluationDependsOn(":example-library")
dependencies {
compile project(path: ':example-library')
}