Generate minimized jar with only used classes

一个人想着一个人 提交于 2019-12-01 16:14:56

Excuse me, maybe i not clearly understood question. Obfuscator tool (i.e. ProGuard) could do that, isn't it? It packs several JARs into one and strips unused classes. If you don't need obfuscation/optimization (to prevent unwanted side-effects) then you could disable them, leaving "shrink" phase enabled.

The maven plugin you can't remember is probably Apache Maven Shade Plugin, there is minimizeJar option. As Andreas_D noticed, this won't include classes, loaded with Class.forName, so you will need to implicity say in configuration, that you need them. Here is how i made maven to include jdbc driver in my single jar:

<filter>
  <artifact>net.sourceforge.jtds:jtds</artifact>
    <includes>
      <include>**</include>
    </includes>
</filter>

In general it is not possible to automatically select all classes that are used by an application. Just think about what we can do with Class.forName(String name) or if we use a dependency injection container and declare types in external configuration files.

I guess if you use Eclipse to JAR the project it gives some options to do that while JARing :) Maybe it will be useful.

Also you can collect your used library classes under a custom library and include this user created library in the project.

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