The Dependency Analysis Gradle Plugin has the following features:
It reports on dependency-related issues such as:
-
… unused dependencies.
-
… used transitive dependencies, which it is advisable you declare directly.
-
… dependencies declared on the wrong configuration (
api
vsimplementation
vscompileOnly
, etc.). -
… unused annotation processors.
For these types of issues, the plugin has auto-remediation capabilities.
It can also report on plugin-related issues such as when:
-
… the
org.jetbrains.kotlin.jvm
plugin is applied but isn’t used. -
… the
com.android.library
plugin is applied but the module could be JVM.
Additionally, the plugin:
-
… will warn when a classpath has duplicate class files.
-
… can trigger downloading of external dependencies, which can help with containerized builds.
-
… print the dominator tree for a project, which can help with discovering "fat" dependencies that are bloating the size of your deployables.
-
… generate a project graph to help visualize the shape of your project.
The plugin can be used to analyze JVM or Android projects, including multi-project builds including both JVM and
Android. It supports Groovy, Java, Kotlin, and Scala on the JVM. For Android, it supports analysis of
com.android.application
, com.android.library
, and com.android.test
modules. It has special handling for
"application" modules, which are those that apply the application
, org.gretty
, or org.springframework.boot
plugins.
Please see the wiki for comprehensive information.
For detailed instructions, see the wiki.
The simplest approach is to add the following:
plugins {
id("com.autonomousapps.build-health") version "<<latest_version>>"
}
and then in the root build script:
dependencyAnalysis {
issues {
all {
onAny {
severity("fail")
}
}
}
}
Important
|
If your project uses Kotlin or Android (or both), then those plugins must also be loaded in the settings script classloader (or a parent). See the wiki for more information |
For a quick start, just run the following:
./gradlew buildHealth
You will probably see output like the following:
> Task :buildHealth FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':buildHealth'. > There were dependency violations. See report at file:///path/to/project/build/reports/dependency-analysis/build-health-report.txt
If you wish to have this (potentially very long) report printed to console, add this to the dependencyAnalysis
DSL:
dependencyAnalysis {
reporting {
printBuildHealth(true)
}
}
From 2.19.0 for releases, and 2.18.1-SNAPSHOT for snapshots, this plugin uses https://central.sonatype.com. To add this plugin to your project, use the following repositories.
pluginManagement {
repositories {
// releases
mavenCentral()
// snapshots
maven(url = "https://central.sonatype.com/repository/maven-snapshots/")
// Once you start using pluginManagement, you should explicitly add this,
// unless you NEVER want to use this repository
gradlePluginPortal()
}
}
You do not have to apply this plugin to all projects via the settings script. It can also be applied to only specific subprojects. In this case, it must also be applied to the root build script.
plugins {
id("com.autonomousapps.dependency-analysis") version "<<latest_version>>"
}
plugins {
id("com.autonomousapps.dependency-analysis")
}
Important
|
If your project uses Kotlin or Android (or both), then those plugins must also be loaded in the root build script classloader (or a parent). See the wiki for more information |
The analysis can be run against individual modules with the projectHealth
task. For example:
./gradlew app:projectHealth
It is common for the plugin to report many issues with your project’s dependency declarations. Since fixing manually can be tedious, the plugin also provides a task to auto-remediate all issues.
./gradlew fixDependencies
The fixDependencies
task is registered on each project where the plugin is applied. Running it as above will run the
task in each subproject. See also
One click dependencies fix.
In some circumstances, it may be considered infeasible to resolve all issues in one pass. Maybe you have a very large
project, or you publish libraries and you know that changing your dependency declarations will also change your
libraries' metadata, which might break consumers. To support this use-case, the the fixDependencies
task takes an
optional flag to tell it to, essentially, make only "safe" changes.
./gradlew fixDependencies --upgrade
With this flag in place, the fixDependencies
task will not remove or "downgrade" any dependency declarations. It will
only add or "upgrade" declarations (e.g., from implementation
to api
).
In an incremental rollout scenario, one could imagine using the --upgrade
flag, then updating all consumers, then
finally removing the flag and removing all unused dependencies.
If the analysis has any bugs, then fixing the dependency declarations make break your build (but this is also the case with manual fixes). If you encounter this, please file an issue.
Additionally, the rewriting functionality is based on a simplified Gradle Groovy DSL grammar, which will fail in the presence of complex Groovy build scripts. The Kotlin DSL grammar has full support for the entire Kotlin language, which makes the rewriting functionality work much better for Gradle Kotlin DSL scripts. There are no plans to do the same for Gradle Groovy DSL.
You may be curious why the plugin is emitting (or not emitting) advice regarding some dependency. You can ask it why:
./gradlew lib:reason --id com.squareup.okio:okio:2.2.2 (1) > Task :lib:reason ---------------------------------------- You asked about the dependency 'com.squareup.okio:okio:2.2.2'. There is no advice regarding this dependency. ---------------------------------------- Shortest path from :lib to com.squareup.okio:okio:2.2.2: :lib \--- com.squareup.okio:okio:2.2.2 Source: main ------------ * Exposes class okio.BufferedSource (implies api).
-
The version string is optional.
For detailed information on how to configure the plugin, see the wiki.
To configure the plugin, use the
dependencyAnalysis
extension.
Below is a summary of the top-level DSL options:
dependencyAnalysis {
// Declare that the plugin should use typesafe project accessors. False by default.
useTypesafeProjectAccessors(true)
// Configure ABI exclusion rules.
abi { ... }
// Configure the severity of issues, and exclusion rules, for potentially the entire project.
issues { ... }
// Configure issue reports.
reporting { ... }
// Configure dependency structure rules (bundles, mapping, etc).
structure { ... }
// Configure usage rules.
usage { ... }
}
As mentioned above, the plugin has a variety of features not strictly related to maintaining a correct dependency graph.
This task will resolve all the external dependencies for a given source set’s compile and runtime classpaths. "Resolving" a dependency will also result in downloading that dependency. A common case where this is useful is when you want to create an image for a containerized build.
./gradlew :<module>:resolveExternalDependenciesMain
where "Main" refers to the name of the source set or Android variant (capitalized).
This will print the dominator tree for the given project. This is useful when looking for "fat" dependencies that contribute significantly to final binary size (such as with a fatjar or when building an image), and in cases where it’s desirable to shrink that binary.
./gradlew :<module>:printDominatorTreeCompileMain
where "Main" refers to the name of the source set or Android variant (capitalized).
This will generate various views of the project graph in your multi-project build, for both compile and runtime classpaths. In addition to the graphviz output, there is also a topological sort, which might be useful for debugging issues with evaluation order.
./gradlew :<module>:projectGraphMain
where "Main" refers to the name of the source set or Android variant (capitalized).
From version 3.0.0, the plugin includes an api definition in api/api.txt
. Any backwards-incompatible change from then
on will result in a major version release. Note that some code is public only due to tooling limitations; most of this
code is in an internal
package, but com.autonomousapps.tasks
is also considered "internal." Usage of any API in
these internal packages is at your own risk.
For typical users who only apply the plugin and run the primary tasks (buildHealth
, projectHealth
, reason
, etc.),
major releases should be treated as non-events. For these users, the "API" is just those primary tasks.
The following is a list of articles / blog posts that have been published discussing this plugin:
This plugin has also been featured in these newsletters:
Podcast episodes about this plugin could be found here:
Youtube videos about this plugin: