2012-01-02 206 views
11

我试图通过使用Maven Shade PluginminimizeJar来最小化UberJar的大小。它看起来像minimizeJar只包括在代码中静态导入的类(我怀疑这是因为我看到LogFactory.class在超级罐子里org\apache\commons\logging\但没有包括impl包的类,因此在运行超级罐时抛出java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl)。配置Maven Shade最小化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> 
+1

'org/apache/commons/logging/**'只能匹配目录,你可能想匹配所有文件。改用'org/apache/commons/logging/**/*'。 – Corubba 2012-01-02 12:00:59

+0

按照你的建议,我尝试了'... logging/**','... logging/**/*','... logging /**/*.*',但都没有成功。我想问题是包括首先包括然后最小化Jar忽略其他所有和捆绑罐子,因为它认为是合适的。 – kunal 2012-01-02 13:24:49

回答

14

这个功能已经被添加到maven的遮阳帘插件的1.6版本(刚刚发布)。 minimJar现在不会删除已经被过滤器明确包含的类。请注意,在过滤器中包括一些工件的类将排除该工件的非指定类,因此请确保包含您需要的所有类。

下面是一个例子插件配置:

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

底层的' **'不适合我。它似乎也包含了很多来自其他软件包的东西。相反,我不得不使用' org/apache/logging/log4j/** Log4j- *' – Quantum7 2014-03-17 16:38:23

10

我使用的是2.0版本的Maven的阴影插件,仍然我不能“最小化”的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的开发人员实现的方式来处理这种情况(我猜是很常见的)。

+0

nice workaround :) – kunal 2013-04-27 14:41:54