配置 Maven Shade minimumJar 以包含类文件 [英] configure Maven Shade minimizeJar to include class files
问题描述
我正在尝试使用 Maven Shade Plugin
的 minimizeJar
来最小化 UberJar 的大小.看起来 minimizeJar
只包含在代码中静态导入的类(我怀疑这是因为我在 orgapachecommons 的 uber jar 中看到了
但没有包含 LogFactory.class
loggingimpl
包的类,因此抛出 java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl
当我运行 uber-jar 时).
I am trying to minimize the UberJar's size by using Maven Shade Plugin
's minimizeJar
. It looks like minimizeJar
only includes classes that are statically imported in the code (I suspect this because I see LogFactory.class
in uber jar at orgapachecommonslogging
but no classes of the impl
package are included, hence throwing java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl
when I run the uber-jar).
无论 minimizeJar
建议什么,我有什么方法可以告诉 Maven 的 Shade 插件将指定的包包含到最终的 jar 中吗?
Is there any way I can tell Maven's Shade plugin to include specified packages into the final jar no matter what the minimizeJar
suggests?
这里是我正在尝试的 pom 片段:
Here the pom snippet of what I am trying:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>org/apache/commons/logging/**</include>
</includes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.myproject.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
推荐答案
此功能已添加到 maven-shade-plugin(刚刚发布)的 1.6 版中.minimumJar 现在不会删除特别包含在过滤器中的类.请注意,在过滤器中包含一些工件的类将排除该工件的非指定类,因此请确保包含您需要的所有类.
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>
这篇关于配置 Maven Shade minimumJar 以包含类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!