I have a large Maven project with many modules and many pom.xml
files. The project has changed and I suspect the pom's contain some unnecessary dependencies. Is there is a command which removes any unused dependencies from a pom?

- 192
- 13
-
2If you're using IntelliJ, see this : http://jonnyzzz.com/blog/2013/05/13/removing-unused-dependencies-in-idea/ In case of dead link : Jonnyzzz Dependencies plugin – Benj Jun 14 '16 at 12:11
-
3@Benj I tried to use this jonnyzz plugin, and it was useless. Most of the time it won't find any unused dependencies, and sometimes when it does it would remove the dependencies from class path not from build.gradle file. I can't even find proper doc to explain how does this plugin actually work. – vaibhav.g Feb 15 '19 at 07:02
-
@vaibhav.g thanks for the update. It seems things did change since 2016 then. – Benj Apr 22 '19 at 17:43
8 Answers
The Maven Dependency Plugin will help, especially the dependency:analyze
goal:
dependency:analyze
analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.
Another thing that might help to do some cleanup is the Dependency Convergence report from the Maven Project Info Reports Plugin.

- 562,542
- 136
- 1,062
- 1,124
-
8
-
172Be carefull with dependency:analyze, some libraries used at runtime are considered as unused. – Nereis Feb 05 '14 at 07:56
-
42To prevent reporting the runtime and provided dependencies as unused, the [`ignoreNonCompile`](https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html#ignoreNonCompile) option can be set to true. – sudeep Nov 12 '15 at 20:00
-
be carefull while using dependency:analyze, even i was using 'ejp api3'depedency but the report shown it was 'unused and declared' – Akhil S Kamath Jul 11 '18 at 10:29
-
To understand the results of analysis, see this answer: "Used undeclared dependencies are those which are required, but have not been explicitly declared as dependencies in your project." - https://stackoverflow.com/questions/4565740/what-are-unused-undeclared-dependencies-in-maven-what-to-do-with-them – Janac Meena May 08 '20 at 14:55
-
I have seen many test dependencies are also labelled as unused unnecessarily. If I remove them my tests don't compile. – Vishal Sep 09 '20 at 11:48
You can use dependency:analyze -DignoreNonCompile
.
This will print a list of "used undeclared" and "unused declared" dependencies (while ignoring runtime
/provided
/test
/system
scopes for unused dependency analysis.)
But be careful while using this:
As some libraries used at runtime
are considered unused!

- 14,222
- 20
- 104
- 125
-
9
-
1You're right some runtime dependencies are decleared as unused but in fact needed. – Smart Coder Feb 14 '19 at 15:21
-
helpful but still not 100% correct. I could see some dependencies used by Groovy but still listed. – vsingh Jul 08 '20 at 14:22
As others have said, you can use the dependency:analyze goal to find which dependencies are used and declared, used and undeclared, or unused and declared. You may also find dependency:analyze-dep-mgt useful to look for mismatches in your dependencyManagement section.
You can simply remove unwanted direct dependencies from your POM, but if they are introduced by third-party jars, you can use the <exclusions>
tags in a dependency to exclude the third-party jars (see the section titled Dependency Exclusions for details and some discussion). Here is an example excluding commons-logging from the Spring dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

- 83,208
- 23
- 172
- 177
-
3(IntelliJ) I used the Jonnyzzz plugin, as commented to the question. Nice plugin, works well with IntelliJ 2016.1.3 – Benj Jun 14 '16 at 12:12
Have you looked at the Maven Dependency Plugin ? That won't remove stuff for you but has tools to allow you to do the analysis yourself. I'm thinking particularly of
mvn dependency:tree

- 268,207
- 37
- 334
- 440
-
1Excellent - thanks (couldn't find what I was looking for as I kept searching for "clean dependencies" and it was throwing up the clean plugin!! but this looks promising.. mvn dependency:analyze) – Oct 04 '09 at 22:56
I had similar kind of problem and decided to write a script that removes dependencies for me. Using that I got over half of the dependencies away rather easily.
http://samulisiivonen.blogspot.com/2012/01/cleanin-up-maven-dependencies.html

- 155
- 1
- 2
-
4Note, this script do not use `mvn dependency:analyze`. It just tries to remove every dependency and checks if `mvn install` works. – alex Nov 25 '16 at 12:09
You can use DepClean https://github.com/castor-software/depclean/
DepClean is a tool to automatically remove dependencies that are included in your Java dependency tree but are not actually used in the project's code.

- 1,845
- 2
- 19
- 28
You can use dependency_cleaner https://github.com/junaidbs/dependency_cleaner This jar will help to identify and remove unwanted dependency from pom. It will automate the process of Removing a dependency and run then check whether the dependency needful

- 1
- 1
If you are using eclipse, right-click on the jar in Maven Dependencies: Select Maven -> Exclude Maven Artifact...

- 34
- 6