Master Maven Dependency Analysis: Spot Unused and Undeclared JARs
This guide explains why Maven dependency analysis is essential, shows how to run mvn dependency:analyze, interprets warnings about used undeclared and unused declared dependencies, and offers practical tips for when and how to clean up your project's pom.xml safely.
Why perform dependency analysis?
After years of working with .Net, Winform, WPF, ASP.NET MVC, and ASP.NET Core, the author shifted to front‑end and operations, now handling private‑cloud projects where vulnerability scans often expose outdated JARs that need urgent fixes or upgrades.
How to run the analysis
For Maven projects, simply execute the built‑in dependency analysis plugin:
<code>mvn dependency:analyze</code>Review the console output, focusing on two sections:
Used undeclared dependencies found
Unused declared dependencies found
Used undeclared dependencies found
This warning means the code uses a class from a JAR that is not declared directly in
pom.xmlbut is pulled in transitively. Add the missing JAR to
pom.xmlto make the dependency explicit.
Unused declared dependencies found
This warning indicates a JAR declared in
pom.xmlis not referenced in the
src/main/javaor
src/test/javasource code. You may remove such entries, but first ensure they are not required by configuration files or extension points, back up the
pom.xml, and verify the project after removal.
When to run the analysis
During new project initialization : Choose required JARs carefully to avoid unnecessary cleanup later.
When refactoring code : Combine refactoring with a dependency audit to catch stale libraries early.
Risks and precautions
The analysis tool may produce false positives, especially for special usage patterns such as annotation processors. Always run comprehensive tests after removing dependencies.
When taking over an old project, do not rush to delete dependencies before understanding the codebase and business logic.
Quick method using IntelliJ IDEA
Open the project directory in IntelliJ IDEA, right‑click the
pom.xmlfile, and select
Analyze Dependenciesfrom the Maven menu. IntelliJ will display the analysis results, allowing you to add missing dependencies or delete unused ones directly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.