2012-03-02 75 views
0

我已經安裝了一個Maven項目,它由兩個子模塊組成,一個是Java Jar模塊,另一個是使用NPanday創建Windows可執行文件。我的構建工作很好。Maven 3:組裝一個包含二進制資源的Jar文件

我遇到的問題是,我想創建一個包含我的Java庫的Jar文件,並且嵌入了Exe文件,以便我可以將它作爲資源從lib中的代碼加載。

看起來程序集插件是要走的路,但我在配置這個時遇到了一些麻煩。我甚至不知道在這種情況下這是否是正確的道路。

有人可能在這裏請指導我正確的道路或給我一個提示,這樣的組裝描述符應該是什麼樣子?

克里斯

+0

是的,程序集插件應該沒問題。你可以顯示你想要的jar文件的內容是什麼? – 2012-03-02 07:46:36

+0

我會回答你的問題在一個主要的「答案」的原因SO我想要搞亂我的linebreaks :-) – 2012-03-02 08:04:19

回答

0

好的...所以我似乎自己解決了一個解決方案。我知道這個問題是相對特殊的再次...因爲我所有的問題似乎是:-)

解決方案是創建一個maven模塊包含PlexusIoResourceCollection的自定義實現,並從components.xml文件放在「META-INF/plexus」目錄中。

加入這個作爲一個依賴於我的組裝插件後,我能夠的exe文件嵌入到我的罐子:-)

這裏談到的組件的代碼:

package npanday.plugin.archiver; 

import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

/** 
* Created with IntelliJ IDEA. 
* User: cdutz 
* Date: 02.03.12 
* Time: 12:04 
*/ 
public class PlexusIoExeResourceCollection extends PlexusIoCompressedFileResourceCollection { 

    @Override 
    protected String getDefaultExtension() { 
     return ".exe"; 
    } 

    @Override 
    protected InputStream getInputStream(File file) throws IOException { 
     // Simply return an InputStream to the resource file. 
     // This will make it embed the source as a whole. 
     return new FileInputStream(file); 
    } 

    @Override 
    public String getPath() { 
     // Without overriding this, the exe would be included with its full path. 
     // This way it is included directly in the root of the result archive. 
     return super.getFile().getName(); 
    } 

} 

這裏在META-INF /叢/ components.xml的配置XML

<component-set> 
    <components> 
     <component> 
       <role>org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection</role> 
       <role-hint>exe</role-hint> 
       <implementation>npanday.plugin.archiver.PlexusIoExeResourceCollection</implementation> 
       <instantiation-strategy>per-lookup</instantiation-strategy> 
      </component> 
    </components> 
</component-set> 

最後使用在我的組裝插件:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.2.1</version> 
      <executions> 
       <execution> 
        <id>make-assembly</id> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      <dependencies> 
       <dependency> 
        <groupId>org.apache.npanday.plugins</groupId> 
        <artifactId>maven-exe-archiver-plugin</artifactId> 
        <version>${npanday.version}</version> 
       </dependency> 
      </dependencies> 
     </plugin> 

希望它能幫助我。

0

嗯,我有一個Java項目,只包含一個測試類現在,因爲我仍然在建立我的構建階段:

模塊de.cware.utils:LIB -psexec客戶端:

  • /de/cware/utils/psexec/client/Test.java

模塊de.cware.utils:LIB-PSEXEC服務: 輸出一個名爲「service.exe」的文件

我想輸出看起來像客戶端jar,但也包含「service.exe」,所以我可以從客戶端jar中的代碼加載它。

模塊de.cware.utis:LIB-PSEXEC組件:

  • /de/cware/utils/psexec/client/Test.java
  • /service.exe
+0

嗯,我實際上設法解決我的問題:解決方案是實現一個自定義PlexusIoResourceCollection,並將其作爲依賴程序集插件。 – 2012-03-02 12:12:25

相關問題