使用 Shade-Plugin 正确最小化 Uber Jar

发布于 2024-12-26 06:21:37 字数 597 浏览 3 评论 0原文

我正在使用 Maven-Shade-Plugin 创建一个可运行的 Uber-jar。 根据此页面上的最后一帧,可以通过以下方式最小化 jar 的大小:

<configuration>
      <minimizeJar>true</minimizeJar>
</configuration>

但此功能不考虑 log4j.properties 文件中声明的类。因此,例如 org.apache.log4j.appender.TimeAndSizeRollingAppender 不包含在 Uber-jar 中,即使它已在 log4j.properties 文件中声明。

我相信Spring也会面临同样的问题。如果我的代码仅引用接口 A 并且我的 Spring 文件包含实现 A 的类 B 的实例化,则 B 可能不会添加到 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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2025-01-02 06:21:37

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

这是一个示例插件配置:

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

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

<filter>
  <artifact>org.yourorg:your-artifact</artifact>
  <includes>
    <include>org/yourorg/yourartifact/api/*</include>
    <include>org/yourorg/yourartifact/util/*</include>
  </includes>
</filter>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文