Reduce jar file size

三世轮回 提交于 2019-12-19 06:50:24

问题


Is there any way to reduce jar file size?

I want a tool that reduces the unused dependencies.

I use maven for dependency management.


回答1:


A JAR file doesn't normally contain dependencies in the Maven sense. So you must be talking about:

  • a WAR or EAR or similar file,
  • a so-called UberJAR file produced by combining lots of JAR files; e.g. using the Maven shade plugin, or
  • dependencies at a finer granularity than Maven modules.

In the first two cases, you can keep out nominally dependent JARs by excluding them, either in the dependency specification, or in the war or shade plugin build descriptor. IIRC, the shade plugin also allows you to exclude specific packages and classes.

The last may require a separate tool to post-process the JAR file. Getting rid of unused classes is the kind of thing that an obfuscator can do. However, you need to be careful not to eliminate classes or class names that are used reflectively; e.g. by a DI / IoC framework or an AOP framework.

(Generally speaking, this kind of tool tries to figure out what classes are used by analysing the dependencies implied by .class file external references. DI / IoC / AOP and so on introduce other kinds of dependency that are not apparent in the .class file structure.)




回答2:


If you like to know the dependencies your project uses just check the maven-dependency-plugin which can be used to analyze the used/unused dependencies.

Check your dependencies via:

mvn dependency:analyze

or take a look at the dep tree like this:

mvn dependency:tree

Or you can take a look into your ide (depending which one you use) for example with Eclipse (Indigo) and the m2e plugin you have a tab "Dependency Hierarchy" which shows the tree of dependencies incl. the transitive dependencies.

In some situation you have to be careful about dependencies which are used by DI frameworks which can't be analyzed by maven-dependency-plugin or by ide plugins.




回答3:


pack200 can drastically reduce the JAR size. But it's hard to use with Maven and impossible to use with an EE container.

Why do you have unused dependencies?



来源:https://stackoverflow.com/questions/10383274/reduce-jar-file-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!