thickosticko
thickosticko

Reputation:

Is there a simple way to remove unused dependencies from a maven pom.xml?

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?

Upvotes: 364

Views: 255113

Answers (8)

Saikat
Saikat

Reputation: 16710

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!

For more details refer to this link

Upvotes: 117

Junaid Kummil
Junaid Kummil

Reputation: 21

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

Upvotes: 2

Martin Monperrus
Martin Monperrus

Reputation: 2051

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.

Upvotes: 4

Usman Mohyuddin
Usman Mohyuddin

Reputation: 32

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

Upvotes: -4

user1132305
user1132305

Reputation: 165

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

Upvotes: 14

Rich Seller
Rich Seller

Reputation: 84028

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>

Upvotes: 50

Pascal Thivent
Pascal Thivent

Reputation: 570295

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.

Upvotes: 272

Brian Agnew
Brian Agnew

Reputation: 272217

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

Upvotes: 39

Related Questions