Reduce jar file size

后端 未结 3 845
天命终不由人
天命终不由人 2021-01-12 17:57

Is there any way to reduce jar file size?

I want a tool that reduces the unused dependencies.

I use maven for dependency management.

相关标签:
3条回答
  • 2021-01-12 18:10

    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.)

    0 讨论(0)
  • 2021-01-12 18:19

    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?

    0 讨论(0)
  • 2021-01-12 18:21

    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.

    0 讨论(0)
提交回复
热议问题