在 ant 构建中包含 hibernate jar 依赖项

发布于 2024-08-29 15:57:10 字数 1972 浏览 0 评论 0原文

我正在尝试为使用 hibernate 的项目编译一个可运行的 jar 文件。我正在尝试构建一个 ant build.xml 文件来简化我的构建过程,但我在将 hibernate3.jar 包含在最终的 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-variable 获取所有这些库 jar 文件的类路径,并使用 pathconvert 和 mapper 展平它们。

我还尝试将清单类路径设置为“.”、“./”并且仅设置库 jar,但没有任何区别。我希望有一个简单的方法可以解决我的问题......

I'm trying to compile a runnable jar-file for a project that makes use of hibernate. I'm trying to construct an ant build.xml file to streamline my build process, but I'm having troubles with the inclusion of the hibernate3.jar inside the final jar-file.

If I run the ant script I manage to include all my library jars, and they are put in the final jar-file's root. When I run the jar-file I get a

java.lang.NoClassDefFoundError: org/hibernate/Session 

error. If I make use of the built-in export to jar in Eclipse, it works only if I choose "extract required libraries into jar". But that bloats the jar, and includes too much of my project (i.e. unit tests).

Below is my generated manifest:

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

And the part of the build.xml looks like this:

<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>

The refids' for the zipfilesets point to the directories in a library directory lib in the root of the project. The manifest.classpath-variable takes the classpath of all those library jar-files, and flattens them with pathconvert and mapper.

I've also tried to set the manifest classpath to ".", "./" and only the library jar, but to no difference at all. I'm hoping there's a simple remedy to my problems...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2024-09-05 15:57:10

由于您无法在类路径中指定 jar-inside-jar,因此这种方式不起作用。
您需要只打包您的代码,然后使用libs jar和启动脚本压缩您的jar,就像

#!/bin/sh
java -cp ./hibernate.jar:./mycode.jar... my.Main

(这是大多数人所做的),或者您可以将所有lib jar解压到包含以下内容的同一目录你的编译类,然后将jar结果目录返回到单个jar文件(这就是maven jar-with-dependencies打包所做的)。

Since you can't specify jar-inside-jar in your classpath, this way won't work.
You need either to jar only your code and then zip your jar with libs jars and launch script, like

#!/bin/sh
java -cp ./hibernate.jar:./mycode.jar... my.Main

(this what most people do), or you may unpack all your lib jars to the same dir which contains your compiled classes and then jar result dir back to single jar file (this what maven jar-with-dependencies packaging do).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文