我有一個應該很容易解決的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插件它現在可以很好地工作。
爲什麼不使您的生活更輕鬆,使用[appassembler - Maven的插件(http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/),其中處理所有的東西已經。它爲windows/unix生成一個腳本,並且會爲你處理所有的類路徑。 – khmarbaise 2013-02-18 18:03:10