本文介绍了配置Maven Shade minimizeJar以包含类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用 Maven Shade插件 minimizeJar 来最小化UberJar的大小。它看起来像 minimizeJar 只包含在代码中静态导入的类(我怀疑这是因为我看到 LogFactory.class 在uber jar中 org\apache\commons\logging\ 但是没有 impl 包的类包含,因此当我运行uber-jar时抛出 java.lang.ClassNotFoundException:org.apache.commons.logging.impl.LogFactoryImpl

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 org\apache\commons\logging\ 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).

有什么方法可以告诉Maven的Shade插件将指定的包包含到最终的jar中,无论 minimizeJar 建议什么?

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的1.6版本中-shade-plugin(刚刚发布)。 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>

这篇关于配置Maven Shade minimizeJar以包含类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 01:14