仅包装依赖项中必要的类

编程入门 行业动态 更新时间:2024-10-26 21:20:04
本文介绍了仅包装依赖项中必要的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用Maven阴影或程序集插件自动确定依赖项jar中需要哪些类,然后仅将这些类复制到构建中.到目前为止,我还没有运气让它自动查找构建所需的类.

How can I use the maven shade or assembly plugin to automatically determine which classes are needed from the dependency jars, and copy ONLY these classes into the build. So far I have had no luck getting it to automatically find which classes will be necessary for my build.

我可以使用 minimizeJar 并包含和排除标签来指定要添加的类,但我真正想要做的是类似的东西:

I can use minimizeJar and include and exclude tags to specify which classes get added but what I really want it to do is something like this:

将我所有类的所有导入都复制到jar中,并复制所有其导入及其所有导入的导入,依此类推.

Copy all of the imports of all of my classes into the jar, and copy all of their imports, and all of their import's imports and so on.

我在想这个问题全错了吗?如何使maven根据所使用的导入项自动最小化jar的大小?(请不要只说这个Maven插件),因为我已经走到了尽头,我需要一个示例来说明如何添加到pom中来完成这些任务.

Am I thinking about the problem all wrong? How can I get maven to automatically minimize the jar size depending on which imports are used? (please don't just say this maven plugin) because I am already at a dead end, I need an example of what to add to my pom to accomplish these tasks.

推荐答案

ProGuard 可以删除未使用的类,甚至可以删除更多.

ProGuard can remove unused classes and even more.

ProGuard是免费的Java类文件收缩器,优化器,混淆器和预验证器.它检测并删除未使用的类,字段,方法和属性.它优化字节码并删除未使用的指令.它使用简短的无意义名称重命名其余的类,字段和方法.

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names.

首先,您应该打包具有依赖项的jar.可以使用maven-assembly-plugin轻松完成:

First, you should package a jar with dependencies. It can be easily done with maven-assembly-plugin:

<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.4</version> <configuration> <archive> <manifest> <mainClass>com.test.HelloWorld</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>

然后配置proguard来优化生成的jar.对于Maven项目,请使用 proguard-maven-plugin :

Then configure proguard to optimize generated jar. For maven projects, use proguard-maven-plugin:

<plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.0.10</version> <executions> <execution> <phase>package</phase> <goals><goal>proguard</goal></goals> </execution> </executions> <configuration> <injar>${project.build.finalName}-jar-with-dependencies.jar</injar> <outjar>${project.build.finalName}-small.jar</outjar> <outputDirectory>${project.build.directory}</outputDirectory> <options> <option>-dontobfuscate</option> <option>-dontwarn com.google.**</option> <option>-keep public class com.test.HelloWorld {public static void main(java.lang.String[]);}</option> </options> <libs> <lib>${java.home}/lib/rt.jar</lib> </libs> </configuration> </plugin>

这些步骤运行后

mvn clean install

并检查 target/< artifact名称> -small.jar -它应仅包含运行时实际使用的类.

and check target/<artifact name>-small.jar - it should contain only classes that are actually used in runtime.

请注意,如果您的代码或依赖项使用反射,则可能会出现一些问题.

更多推荐

仅包装依赖项中必要的类

本文发布于:2023-10-04 16:47:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!