Matt Vukas
Matt Vukas

Reputation: 3295

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?

Upvotes: 9

Views: 7484

Answers (2)

Michael Lucas
Michael Lucas

Reputation: 783

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!

Upvotes: 1

JP Belanger
JP Belanger

Reputation: 119

I don't know that there are generally accepted best practices around this, given that anything from introspection to services could break at runtime. Unit tests could help you, but if you don't have good coverage, they could give you a false sense of security. You'd still have to check with function tests (hopefully automated) after you remove every package.

Upvotes: 0

Related Questions