Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to (safely) remove unnecessary Maven dependencies in Eclipse?

I'm working on a Java project in Eclipse, using Maven to build and manage dependencies. The project is spread across 5 Eclipse projects, one of those being the parent POM. I'm working on a server implementation that is based off of a much more complicated server that another team implemented. So I based my work off of their pre-existing code and POM files, and now have many unnecessary dependencies in the POMs across these Eclipse projects.

Relatively speaking, I'm a Maven beginner, but I am familiar with this command:

mvn dependency:analyze

When I run this command using the Eclipse Maven plugin, I will get a long list of "Unused declared dependencies," but when I attempt to remove a few of them, my program will break, sometimes in mysterious ways.

Is there a generally accepted, best-practices way to deal with this problem? Or am I resigned to removing these (probably) unused dependencies one-by-one, making sure nothing is broken after each one is removed?

like image 969
Matt Vukas Avatar asked Jul 08 '13 13:07

Matt Vukas


People also ask

How do I delete a dependency in Maven?

Open the pom file and click on dependency Hierarchy. Select the the jar you want to delete. Right click and click on Exclude Maven Artifact.

Does Maven clean remove dependencies?

It only cleans the project. Show activity on this post. Show activity on this post. With the help of Purging local repository dependencies you need to do that.

How do I clean up .m2 folder?

m2 folder? Simply delete the . m2repository folder. It will get created automatically once you compile the project.


1 Answers

You could try running your code with the java -verbose:class option turned on. This will generate output showing you where (which jar file) each class gets loaded from. (Note: on Sun JREs this gets written to standard out; I think on IBM JREs it gets written to standard error.) Here's an example of output:

[Loaded junit.framework.AssertionFailedError from file:/D:/Documents%20and%20Settings/mike/.m2/repository/junit/junit/3.8.2/junit-3.8.2.jar]
[Loaded junit.framework.ComparisonFailure from file:/D:/Documents%20and%20Settings/mike/.m2/repository/junit/junit/3.8.2/junit-3.8.2.jar]
[Loaded org.jmock.core.SelfDescribing from file:/D:/Documents%20and%20Settings/mike/.m2/repository/jmock/jmock/1.2.0/jmock-1.2.0.jar]
[Loaded org.apache.log4j.spi.Configurator from file:/D:/Documents%20and%20Settings/mike/.m2/repository/log4j/log4j/1.2.9/log4j-1.2.9.jar]
[Loaded org.apache.log4j.xml.DOMConfigurator from file:/D:/Documents%20and%20Settings/mike/.m2/repository/log4j/log4j/1.2.9/log4j-1.2.9.jar]

As long as you run through most of your program's logic (so that the required classes get loaded), you can safely assume that any jars that are not mentioned in the verbose:class output, can be removed as a maven dependency.

You could also do a search & replace on the verbose:class output, turning it into csv for example, then bringing it into a spreadsheet program to sort/group by the jar files.

Still a fair bit of manual effort, but at least it would give you somewhere to start!

like image 58
Michael Lucas Avatar answered Sep 18 '22 08:09

Michael Lucas