是否存在与mvn dependency:analyze等效的Gradle依赖分析插件?
dependency:analyze Absolutely! Gradle has several tools that match the functionality of Maven's dependency:analyze goal, specifically focusing on the three dependency categories you care about: used & declared, used but not declared, and unused but declared. Let’s walk through the most reliable options:
Nebula Dependency Analyze Plugin
This is the most popular and direct replacement for Maven’s dependency analysis. It’s maintained by Netflix’s Nebula team and is purpose-built to flag exactly those dependency issues you’re looking for.
Setup
Add the plugin to your build.gradle (or build.gradle.kts) using the plugins block:
plugins { id 'java' id 'nebula.dependency-analyze' version '11.3.0' // Use the latest available version }
Run the Analysis
Execute the task from your terminal:
./gradlew analyzeDependencies
What It Reports
The plugin will generate both console output and an HTML report (in build/reports/dependency-analysis) that breaks down dependencies into your three categories:
- Used and declared: These are marked as correctly configured, with no warnings.
- Used but not declared: The plugin will flag these as undeclared dependencies (it fails the build by default, but you can configure it to just warn instead). This helps catch transitive dependencies you’re relying on directly but haven’t explicitly added to your
dependenciesblock. - Unused but declared: These are listed as unused declared dependencies, giving you a clear list of dependencies you can safely remove to clean up your build file.
You can also customize behavior (like ignoring specific dependencies or adjusting failure rules) via plugin configuration if needed.
Gradle Built-in inspectDependencies Task (Gradle 7.4+)
If you prefer to stick with built-in Gradle tools without adding third-party plugins, the inspectDependencies task (introduced in Gradle 7.4) offers basic dependency usage analysis.
Run It
Execute:
./gradlew inspectDependencies
What It Provides
It generates a report in build/reports/dependency-inspection that includes details on which dependencies are used in your code vs. which are declared. While it’s not as granular or configurable as the Nebula plugin, it’s a good option if you want a lightweight, no-additional-plugin solution.
Final Recommendation
For feature parity with Maven’s dependency:analyze, go with the Nebula Dependency Analyze plugin—it’s the most mature and fully matches the functionality you’re asking for. The built-in inspectDependencies works for quick checks but lacks some of the customization and clarity of the Nebula tool.
内容的提问来源于stack exchange,提问作者Ryanqy

