2012-01-02 204 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