配置Maven Shade minimizeJar以包含类文件

kda*_*bir 12 java jar maven

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

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

这里是我正在尝试的pom片段:

    <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>
Run Code Online (Sandbox Code Playgroud)

Sha*_*ane 15

此功能已添加到maven-shade-plugin的1.6版本(刚刚发布).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>
Run Code Online (Sandbox Code Playgroud)


jam*_*ing 11

我使用的是Maven Shade插件的2.0版本,但在"最小化"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
    };
}
Run Code Online (Sandbox Code Playgroud)

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