Generate minimized jar with only used classes

时光总嘲笑我的痴心妄想 提交于 2019-12-19 13:48:43

问题


I'm in need of creating the minimal jar of utils library for use in Android. I'm using some methods from apache commons libraries (such as IOUtils, StringUtils). However, each such usage makes me import the whole library (commons-lang, commons-io etc.) which is absolutely acceptable under Tomcat (war's are mamoot-sized anyway), but absolutely unacceptable for Android project.

So, my aim is, to pack all used classes from dependencies into one jar - but only that classes that are needed. I remember once being in touch with maven plugin that done that task, unfortunatelly I can't remember its name nor find it via Google.

So please, do you know maven plugin that will do such minimization of dependencies, or any stand-alone tool that will do the same?


回答1:


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.




回答2:


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>



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/9518400/generate-minimized-jar-with-only-used-classes

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