使用Shade-Plugin正确地最小化Uber Jar [英] Minimize an Uber Jar correctly, Using Shade-Plugin
问题描述
我使用Maven-Shade-Plugin创建一个可运行的Uber-jar。
根据此页<上的最后一帧/ a>,可以通过使用以下方式最小化jar的大小:
I am using the Maven-Shade-Plugin to create a runnable Uber-jar. According to the last frame on this page, the size of the jar can be minimized by using:
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
但是这个功能没有考虑log4j.properties文件中声明的类。因此,例如 org.apache.log4j.appender.TimeAndSizeRollingAppender
未包含在Uber-jar中,即使它已在log4j.properties文件中声明。
But this feature does not take into consideration the classes that are declared in the log4j.properties file. Hence, e.g. org.apache.log4j.appender.TimeAndSizeRollingAppender
is not included in the Uber-jar, even though it’s declared in the log4j.properties file.
我相信我会遇到与Spring相同的问题。如果我的代码只引用接口A而我的Spring文件包含实现A的B类实例化,那么B可能不会被添加到jar中,因为它不在代码中。
I believe I will face the same problem with Spring. If my code only refers to interface A and my Spring file contains an instantiation of class B that implements A, then B might not be added to the jar, since it’s not in the code.
如何解决这个问题?
推荐答案
此功能已添加到maven-shade的1.6版本中插件(刚刚发布)。 minimizeJar现在不会删除过滤器中特别包含的类。请注意,在过滤器中包含一些工件的类将排除该工件的非指定类,因此请确保包含所需的所有类。
This functionality has been added to version 1.6 of the maven-shade-plugin (just released). minimizeJar will now not remove classes that have been specifically included with filters. Note that including some of an artifact's classes in a filter will exclude non-specified classes for that artifact, so be sure to include all the classes that you need.
这是一个示例插件配置:
Here's an example plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>log4j:log4j</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
要仅包含特定类,请使用过滤器中类名中的路径斜杠将它们添加为包含(再次,非包含类将被自动排除。)
To only include specific classes, add them as includes using path slashes in the class name in a filter (again, non-included classes will be automatically excluded).
<filter>
<artifact>org.yourorg:your-artifact</artifact>
<includes>
<include>org/yourorg/yourartifact/api/*</include>
<include>org/yourorg/yourartifact/util/*</include>
</includes>
</filter>
这篇关于使用Shade-Plugin正确地最小化Uber Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!