2010-04-22 71 views
2

我正在嘗試爲使用hibernate的項目編譯可運行的jar文件。我試圖構建一個ant build.xml文件來簡化我的構建過程,但是我在最終jar文件中包含hibernate3.jar時遇到了麻煩。在ant build中包含休眠jar依賴關係

如果我運行ant腳本,我設法包含我所有的庫jar,並將它們放入最終的jar文件的根目錄。當我運行jar文件時,我得到一個

java.lang.NoClassDefFoundError: org/hibernate/Session 

錯誤。如果我在Eclipse中使用內置導出到jar,它只有在我選擇「將所需的庫提取到jar中」時才起作用。但是這會使罐子膨脹,並且包含了太多的項目(即單元測試)。

下面是我生成的清單:

Manifest-Version: 1.0 
Main-Class: main.ServerImpl 
Class-Path: ./ antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar 
hibernate3.jar javassist-3.9.0.GA.jar jta-1.1.jar slf4j-api-1.5.11.jar 
slf4j-simple-1.5.11.jar mysql-connector-java-5.1.12-bin.jar rmiio-2.0.2.jar 
commons-logging-1.1.1.jar 

而且build.xml文件的部分看起來像這樣:

<target name="dist" depends="compile" description="Generates the Distribution Jar(s)"> 
    <mkdir dir="${dist.dir}" /> 
    <jar destfile="${dist.dir}/${dist.file.name}.jar" basedir="${build.prod.dir}" filesetmanifest="mergewithoutmain"> 
     <manifest> 
      <attribute name="Main-Class" value="${main.class}" /> 
      <attribute name="Class-Path" value="./ ${manifest.classpath} " /> 
      <attribute name="Implementation-Title" value="${app.name}" /> 
      <attribute name="Implementation-Version" value="${app.version}" /> 
      <attribute name="Implementation-Vendor" value="${app.vendor}" /> 
     </manifest> 
     <zipfileset refid="hibernatefiles" /> 
     <zipfileset refid="slf4jfiles" /> 
     <zipfileset refid="mysqlfiles" /> 
     <zipfileset refid="commonsloggingfiles" /> 
     <zipfileset refid="rmiiofiles" /> 
    </jar> 
</target> 

爲zipfilesets的refids'指向的目錄庫中的目錄lib在項目的根目錄中。 manifest.classpath變量接受所有這些庫jar文件的類路徑,並使用pathconvert和mapper將它們弄平。

我也嘗試設置清單類路徑爲「。」,「./」,只有庫jar,但沒有任何區別。我希望有一個簡單的補救措施,我的問題...

回答

2

因爲你不能指定jar-inside-jar在你的類路徑,這種方式將無法正常工作。 你需要要麼罐子只代碼,然後用庫瓶和啓動腳本壓縮您的罐子,就像

(這是大多數人做的),或者你可以解開所有的lib罐子一樣其中包含你的編譯類,然後將jar結果返回到單個jar文件(這是maven jar -with-dependencies包裝所做的)。

+0

謝謝您的澄清。我選擇將所有lib庫解壓到我的build-dir中,並將所有類與項目類一起重新打包。 我使用了zipfileset:,但是有沒有更簡單的方法在目錄中包含多個jar文件,而不必調用zipfileset爲每個我想包括的jar文件? – Patrick 2010-04-22 12:58:17

+0

你總是可以寫sh腳本,它會做'jar xf * jar; jar cf my-app-with-libs.jar *'。 不過,我會推薦第一種方法 - zip將自動解壓縮並通過附帶的shell腳本啓動。使用第二種方法,您必須照顧來自不同庫的具有相同FQCN的類(如果有的話)。 – 2010-04-22 13:48:23

+0

好的,非常感謝您的幫助。實際上,我選擇將清單類路徑指向一個附帶的lib子文件夾,我在其中複製了所有必需的jar文件,現在它效果很好。 – Patrick 2010-04-22 16:16:05