使用 Shade-Plugin 正确最小化 Uber Jar [英] Minimize an Uber Jar correctly, Using Shade-Plugin

查看:38
本文介绍了使用 Shade-Plugin 正确最小化 Uber Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Maven-Shade-Plugin 创建一个可运行的 Uber-jar.根据本页的最后一帧,可以使用以下方法最小化罐子的大小:

<预><代码><配置><minimizeJar>true</minimizeJar></配置>

但是这个特性没有考虑到 log4j.properties 文件中声明的类.因此,例如org.apache.log4j.appender.TimeAndSizeRollingAppender 不包含在 Uber-jar 中,即使它在 log4j.properties 文件中声明.

我相信我会在使用 Spring 时面临同样的问题.如果我的代码只引用接口 A 并且我的 Spring 文件包含实现 A 的类 B 的实例化,那么 B 可能不会添加到 jar 中,因为它不在代码中.

我该如何解决这个问题?

解决方案

此功能已添加到 maven-shade-plugin(刚刚发布)的 1.6 版中.minimumJar 现在不会删除特别包含在过滤器中的类.请注意,在过滤器中包含一些工件的类将排除该工件的非指定类,因此请确保包含您需要的所有类.

这是一个示例插件配置:

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>1.6</version><执行><执行><phase>包</phase><目标><目标>阴影</目标></目标><配置><minimizeJar>true</minimizeJar><过滤器><过滤器><artifact>log4j:log4j</artifact><包括><include>**</include></包括></过滤器><过滤器><artifact>commons-logging:commons-logging</artifact><包括><include>**</include></包括></过滤器></过滤器></配置></执行></执行></插件>

要仅包含特定类,请在过滤器中使用类名中的路径斜杠将它们添加为包含(同样,未包含的类将被自动排除).

<artifact>org.yourorg:your-artifact</artifact><包括><include>org/yourorg/yourartifact/api/*</include><include>org/yourorg/yourartifact/util/*</include></包括></过滤器>

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>

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.

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.

How can I solve this problem?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆