Ant 在构建可运行的 jar 时卡住 [英] Ant gets stuck while building runnable jar

查看:21
本文介绍了Ant 在构建可运行的 jar 时卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又是我.我一直在尝试使用 ant 脚本将 java 项目创建到可运行的 jar 中.这是我的 build.xml

it's me again. I've been trying to create java project into a runnable jar using ant script. This is my build.xml

<project name="simple-app" basedir="." default="main">
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="app.App" />

    <path id="classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar" />
    </fileset>
            <dirset dir="${build.dir}">
            <include name="classes"/>
    </dirset>
</path>

<target name="clean">
    <delete dir="${build.dir}" />
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac source="1.7" target="1.7" includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
        </manifest>
    </jar>
</target>


<target name="run" depends="jar">
    <java classname="${main-class}">
        <classpath>
            <path refid="classpath" />
            <path location="${jar.dir}/${ant.project.name}.jar" />
        </classpath>
    </java>
</target>

<target name="main" depends="clean,run"/>
</project>

一切顺利,但是当它开始运行"时它会卡住.终端说运行:什么也没发生.等了30分钟,什么都没有.我尝试了在互联网上找到的许多其他选项,但这些选项要么导致相同的延迟,要么抛出 ClassNotFoundException.

All goes well, but when it gets to "run" it gets stuck. Terminal says run: and nothing happens. Waited for 30 minutes, nothing. I tried many other options I found around the internet but those resulted either in the same lag, or threw ClassNotFoundException.

我真的不知道该怎么办.当我用 Eclipse 制作文件时,一切正常.任何人都可以帮助我吗?这可能是完全愚蠢的事情,但我只是没有看到它.非常感谢.

I seriously don't know what, to do. When I make the file with Eclipse, all works fine. Anyone can help me? It's propably something totally stupid, but I just don't see it. Thank you very much.

推荐答案

要修复 ClassNotFoundException 异常,您需要在 jar 的清单中包含类路径.manifestclasspath 任务非常有用:

To fix the ClassNotFoundException exception you need to include the classpath in the jar's manifest. The manifestclasspath task comes in very useful:

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />

    <manifestclasspath property="jar-classpath" jarfile="${jar.dir}/${ant.project.name}.jar">
      <classpath refid="classpath" />
    </manifestclasspath>

    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
            <attribute name="Class-Path" value="${jar-classpath}" />
        </manifest>
    </jar>
</target>

这使您可以按如下方式调用 jar:

This enables you to invoke the jar as follows:

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

或从命令行:

java -jar /path/to/myproject.jar

最后这可能无法解释为什么您的构建挂起......代码是否有可能进入等待状态?

In the end this may not explain why your build is hanging.... Is it possible the code is going into a waiting state?

这篇关于Ant 在构建可运行的 jar 时卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆