【问题标题】:configure Maven Shade minimizeJar to include class files配置 Maven Shade minimizeJar 以包含类文件
【发布时间】:2012-01-31 16:45:59
【问题描述】:

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

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

这里是我正在尝试的 pom sn-p:

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

【问题讨论】:

  • org/apache/commons/logging/** 只匹配目录,你可能想匹配所有文件。请改用org/apache/commons/logging/**/*
  • 按照您的建议,我尝试了...logging/**...logging/**/*...logging/**/*.*,但都没有奏效。我想问题是首先包含 include 然后 minimizeJar 忽略其他所有内容并按照它认为合适的方式捆绑 jar。

标签: java maven jar


【解决方案1】:

我正在使用 2.0 版本的 Maven Shade 插件,但在“最小化”JAR 之后仍然无法包含类。

作为一种解决方法,我想到的唯一一件事是创建对所需类的引用,以避免使用最小化代码来摆脱它们。即:

/*
 * This block prevents the Maven Shade plugin to remove the specified classes
 */
static {
    @SuppressWarnings ("unused") Class<?>[] classes = new Class<?>[] {
        JButton.class,
        JMenu.class,
        JMenuBar.class,
        JMenuItem.class
    };
}

我希望 Maven 开发人员实现一种方法来处理这种情况(我猜这很常见)。

【讨论】:

    【解决方案2】:

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

    这是一个示例插件配置:

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

    【讨论】:

    • 底层&lt;include&gt;**&lt;/include&gt; 对我不起作用。它似乎也包含了其他软件包中的很多东西。相反,我不得不使用&lt;include&gt;org/apache/logging/log4j/**&lt;/include&gt; &lt;include&gt;Log4j-*&lt;/include&gt;
    • 仍然不适用于 3.2.4 版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2023-03-15
    • 1970-01-01
    • 2020-12-07
    • 2014-07-23
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多