Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to optimize maven dependencies automatically?

I am working on a big project that consists of about 40 sub-projects with very not optimized dependencies. There are declared dependencies that are not in use as well as used but undeclared dependencies. The second case is possible when dependency is added via other dependency.

I want to remove redundant and add required dependencies. I ran mvn dependency:analyze and got a long list of warnings I have to fix now.

I wonder whether there is maven plugin or any other utility that can update my pom.xml files automatically. I tried to do it manually but it takes a lot of time. It seems it will take a couple of days of copy/paste to complete the task.

In worse case I can write such script myself but probably ready stuff exists?

Here is how mvn dependency:analyze reports dependency warnings:

[WARNING] Used undeclared dependencies found:
[WARNING]    org.apache.httpcomponents:httpcore:jar:4.1:compile
[WARNING] Unused declared dependencies found:
[WARNING]    commons-lang:commons-lang:jar:2.4:compile
[WARNING]    org.json:json:jar:20090211:compile
like image 925
AlexR Avatar asked Jul 11 '12 14:07

AlexR


People also ask

Does Maven download dependencies automatically?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.

How does Maven handle the task of dependency management?

Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.


1 Answers

I would not say: with very not optimized dependencies. it's simply someone has not done his job well, cause defining dependencies which are not used shows someone didn't understand what a build tools is and how its working. That can be compared with a Java file which contains many unused imports. In the case of the unused imports in a Java sources this can simply be handle by the IDE but for dependencies in Maven there does not exist such a simple way as already been expressed the problem are kinds of DI etc. which makes this job hard. You can try to output the result of dependency:analyze into a script (there exist an option for that goal) and afterwards test the resulting build after cleaning up the dependencies.

It might be a good idea to run

mvn dependency:analyze -DscriptableOutput=true 

which produces output which can be very simple extracted from the output and can be used for further processing like using as input for the versions-maven-plugin (with some pre conversion).

like image 162
khmarbaise Avatar answered Sep 18 '22 16:09

khmarbaise