有什么办法可以减小 jar 文件的大小?
我想要一个工具来减少未使用的依赖项。
我使用 maven 进行依赖管理。
A JAR file doesn't normally contain dependencies in the Maven sense. So you must be talking about:
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.)
如果您想知道项目使用的依赖项,只需检查maven-dependency-plugin即可用于分析使用/未使用的依赖项。
通过以下方式检查您的依赖项:
mvn dependency:analyze
或者像这样查看 dep 树:
mvn dependency:tree
或者您可以查看您的 ide(取决于您使用哪个),例如使用 Eclipse(Indigo)和 m2e 插件,您有一个选项卡“Dependency Hierarchy”,它显示了依赖关系树,包括。传递依赖。
在某些情况下,您必须小心 DI 框架使用的依赖项,这些依赖项无法通过 maven-dependency-plugin 或 ide 插件进行分析。
pack200可以大大减少 JAR 的大小。但是它很难与 Maven 一起使用,并且不可能与 EE 容器一起使用。
为什么你有未使用的依赖项?