Reduce jar file size

后端 未结 3 931
天命终不由人
天命终不由人 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条回答
  •  Happy的楠姐
    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.)

提交回复
热议问题