How to structure a growing Gradle project with smart dependency management?
How to structure a growing Gradle project with smart dependency management?
This repo contains a Gradle project structure with:
The main branch contains everything for a traditional Java project.
The structure though, is good for any kind of project you may build with Gradle
(Kotlin, Groovy, Scala, ...).
[!NOTE] There are adjustments on other branches of this repo that show how the setup can be varied:
[!TIP] There are two sister repositories that share the same example with a different focus in the build setup
- idiomatic-gradle Focus on structuring builds for both mono-repo or multi-repo setups
- ️ javarcade Focus on structured dependency management with the JavaRCA recipe
…Select a Component Type by using the corresponding convention plugin and define the dependencies of the module. For example:
plugins {
id("org.example.gradle.component.library") // This is a 'library'
id("org.example.gradle.feature.publish") // Build feature only in this 'library' Module
}
dependencies {
api(projects.coruscant) // Depends on another Module of our project
implementation(libs.guava) // Depends on a 3rd party Module
}Convention plugins are used to configure each aspect of the build centrally. To keep it structured, we put them into four categories: Base, Feature, Check, Report. Below you find all plugins listed. For more details, inspect the corresponding plugin files. Understanding Gradle videos that cover related topics are linked below each plugin file.
Base plugins need to be used in all Modules to establish a certain foundation for the setup. For example, the same dependency management configuration should be applied everywhere to consistently use the same 3rd party libraries everywhere.
Each feature plugin configures one aspect of building the software – like compiling code or testing code.
Report plugins add reporting functionality to discover potential issues with the software or the build setup. They may generate data that is picked up and displayed by external tools like Develocity or Dependency Track. More reporting tools may be integrated in this category. Report plugins are not necessarily needed to build a working software.
Check plugins help with keeping the software maintainable over time. They check things like the dependency setup or code formatting. More style checkers or static code analysis tools could be added in this category. Check plugins are not necessarily needed to build a working software.
Component plugins combine plugins from all categories above to define Component Types that are then used in the build.gradle.kts
files of the individual Modules of our software.
The Gradle TestKit can be used to test plugins. This can be helpful to enforce a certain structure, e.g. by testing if each plugin works on its own. And if you add custom tasks and advanced logic, you can add tests for that. As example, this project contains one test class: ConventionPluginTest.kt
There is also a help task that you can use to get a diagram of the convention plugins defined in the project:
./gradlew :aggregation:analysePluginApplicationOrder
The task generates a PlantUML file that you can render, for example, with the PlantUML IntelliJ plugin.
No open issues yet, or sync has not completed.