2013-02-14 30 views
1

我有一個應該很容易解決的maven問題,但是證明有點困難。Maven:構建可運行jar,然後使用maven-assembly-plugin添加到zip文件

我有建立一個罐子,需要一個罐子能夠執行一類com.foo.bar.MainClass項目,但我需要能夠像

some-product-1.0.0.zip 
    bin/ 
    some-product.sh 
    lib/ 
    some-product-1.0.0.jar 
    dependency1.jar 
    dependency2.jar 
有一些腳本和它的依賴一起部署

我可以很容易地製作這個結構。我pom.xml看起來像

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <configuration> 
     <archive> 
      <manifest> 
       <mainClass>com.foo.bar.MainClass</mainClass> 
       <addClasspath>true</addClasspath> 
      </manifest> 
     </archive> 
     <descriptors> 
      <descriptor>assembly.xml</descriptor> 
     </descriptors> 
    </configuration> 
</plugin> 

和我assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <id>bin</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <files> 
     <file> 
      <source>src/main/scripts/some-product.sh</source> 
      <outputDirectory>/bin</outputDirectory> 
     </file> 
    </files> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>/lib</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact> 
      <scope>compile</scope> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

而且some-product.sh看起來像

SCRIPTLOC=$(readlink -f $(dirname "${BASH_SOURCE[0]}")) 
LIBS="$(dirname $SCRIPTLOC)/lib" 
# builds a : joined list of all the jars in the lib directory 
MYCLASSES=$(echo $LIBS/*.jar | tr ' ' ':') 

java -cp "$CLASSPATH:$MYCLASSES" com.foo.bar.MainClass 

,但我得到了什麼,當我跑了劇本

Exception in thread "main" java.lang.NoClassDefFoundError: com/foo/bar/MainClass 
Cause by: java.lang.ClassNotFoundException com.foo.bar.MainClass 
    at ... 
Could not find the main class: com.foo.bar.MainClass. Program will exit. 

當我檢查了包含類的jar,清單隻有

Manifest-Version: 1.0 
Archiver-Version: Plexus Archiver 
Created-By: Apache Maven 
Built-By: <myname> 
Build-Jdk: 1.6.0_27 

告訴我,它沒有主類添加到清單。

我已經搜索了這個問題很長一段時間沒有運氣,程序集插件的網站上的例子是可怕的。我怎樣才能得到maven正確建立這個罐子把它添加到zip?

編輯:我確實需要行家-JAR-插件在我的POM,但我真正的問題是,我用Cygwin和類路徑分隔符,預計是;:。我的解決方案是編寫一些代碼來確定要使用哪個類路徑分隔符,並且使用jar插件它現在可以很好地工作。

+0

爲什麼不使您的生活更輕鬆,使用[appassembler - Maven的插件(http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/),其中處理所有的東西已經。它爲windows/unix生成一個腳本,並且會爲你處理所有的類路徑。 – khmarbaise 2013-02-18 18:03:10

回答

3

兩件事情我看到的是

  1. classpath設置不正確。

    SCRIPTLOC=$(readlink -f $(dirname "${BASH_SOURCE[0]}")) 
    

    這裏SCRIPTLOC不是根目錄。這是<the_complete_path_from_root>/bin

    LIBS="$(dirname $SCRIPTLOC)/lib" 
    

    這最後的計算結果爲<the_complete_path_from_root>/bin/lib

    而你後來添加lib文件夾它,這實際上並不包含你的罐子。所以,要添加到類路徑中的正確路徑是lib文件夾,其中存在jar,那麼它將能夠找到com.foo.bar.MainClass

  2. 要將Main類添加到清單文件中,您需要將以下內容添加到pom.xml文件中。

    <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-jar-plugin</artifactId> 
        <configuration> 
         <archive> 
         <manifest> 
          <mainClass>com.someclass.Main</mainClass> 
          <packageName>com.someclass</packageName> 
         </manifest> 
         <manifestEntries> 
           <mode>development</mode> 
           <url>${pom.url}</url> 
          </manifestEntries> 
         </archive> 
         </configuration> 
        </plugin> 
    
+0

1.類路徑肯定是正確的設置,我以前已經在回聲聲明,以確保。 'dirname'命令會得到父目錄([見文檔](http://linux.die.net/man/1/dirname)) 2.我試過了,但是我得到了同樣的錯誤,NoClassDefFoundException – bheklilr 2013-02-14 17:48:09

+1

好吧,所以它是你需要添加的'maven-jar-plugin'代碼片段,用於將主類包含在清單文件中。 – mtk 2013-02-14 17:50:54

+0

對不起,我在完成它之前意外地提交了我的評論(總是忘記他們不是多行並按回車鍵)。我試過這個片段,但它仍然給我同樣的錯誤=/ – bheklilr 2013-02-14 17:55:22

相關問題