2

If I would like to write a program to automatically exclude not-used jar files from a maven project (regardless of what programming language I will use), how should I get start?

Currently, I have a stupid idea. Except the declared jar files, every time excluding a node of the dependency tree top down, then compile and run the project to see whether this jar file could be excluded.

I cannot find any other better ideas and I need help. Please share me with whatever come into your minds. If you know somebody else has done this before, please post the link to the source code or share with me its ideas. Thanks!!!!

By the way, what if I want to write a program automatically solving the dependency conflict problems? Is this possible? I know Such problems have bothered java developers a lot.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
Zhaoduo W.
  • 31
  • 2
  • https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html – Dmitry Apr 27 '16 at 12:19
  • 1
    Possible duplicate of [Is there a simple way to remove unused dependencies from a maven pom.xml?](http://stackoverflow.com/questions/1517611/is-there-a-simple-way-to-remove-unused-dependencies-from-a-maven-pom-xml) – Dmitry Apr 27 '16 at 12:20
  • Not exactly. I know a dependency plugin exists that might help, but one has to run the command manually to decide which dependency should be excluded. However, I want to write a program doing this automatically for me. – Zhaoduo W. Apr 27 '16 at 14:05
  • Wait for java 9, I believe the "Java Platform Module System" is designed to do that kind of thing, but better. – Bill K Aug 15 '17 at 20:51

2 Answers2

1

The idea of compiling for every removed dependency is good, but don't forget to launch the program also (some dependencies are required at runtime only).

I had to do that a few times, but I did manually (not automatically with a program), i removed all dependencies and added them one by one until code compiles, and until the program run OK.

Julien C
  • 34
  • 2
0

The problem with approaches like this is the fact that even though everything might compile correctly, you are missing resources during runtime.

That might be the case, if you load classes and stuff with help of the Classloader or if you are using reflection to access classes and methods. This gets even worse if you look at more dynamic languages aside of Java: the compiler won't help you that much anymore

J. Dow
  • 563
  • 3
  • 13