ant:生成的JAR无法执行

时间:2013-02-09 00:05:00

标签: java ant jar javac

我编写/复制了一个简单的ant示例,并尝试使用它部署Java程序。我的构建文件bsp0201.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project name="bsp0201" default="main" basedir=".">
    <property name="dir.src" value="./source" />
    <property name="dir.build" value="./classes" />
    <property name="dir.lib" value="./lib" />
    <path id="cp">
        <pathelement path="${classpath}" />
        <pathelement location="${dir.build}" />
    </path>
    <target name="main" depends="prepare, compile, run" />
    <target name="prepare">
        <mkdir dir="${dir.build}" />
        <delete>
            <fileset dir="${dir.build}" includes="**/*" />
        </delete>
    </target>
    <target name="compile">
        <javac classpathref="cp" destdir="${dir.build}" srcdir="${dir.src}" includes="**/*.java" includeantruntime="false" />
        <mkdir dir="${dir.lib}" />
        <jar destfile="${dir.lib}/ae.jar">
            <fileset dir="${dir.build}" includes="**/*.class" />
        </jar>
    </target>
    <target name="run" if="test">
        <java classname="main.GeoAnalyzerMain" classpath="${dir.lib}/ae.jar">
            <arg line="${test}" />
        </java>
    </target>
</project>

我的蚂蚁突击队&amp;结果输出:

# ant -f bsp0201.xml run -Dtest=Echo
Buildfile: /var/www/sandbox/ant/bsp0201/bsp0201.xml

run:

BUILD SUCCESSFUL
Total time: 1 second

Ant生成文件夹,类文件和JAR文件ae.jar。但是这个JAR文件的大小只有19.1 KB(而不是297 KB的JAR文件,当我用eclipse生成它时)我无法执行它。我做错了什么?

THX

2 个答案:

答案 0 :(得分:0)

是的,比较JAR是个好主意。 :)在这里你可以看到比较结果:

JARs comparison

Ant-JAR中缺少文件夹/ img和/.classpath。为什么Ant不包含它们?如何让Ant包含丢失的文件/文件夹?

答案 1 :(得分:0)

我编辑了构建XML:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="bsp0201" default="main" basedir=".">
    <property name="dir.src" value="./source" />
    <property name="dir.build" value="./classes" />
    <property name="dir.lib" value="./lib" />
    <path id="cp">
        <pathelement path="${classpath}" />
        <pathelement location="${dir.build}" />
    </path>
    <target name="main" depends="prepare, compile, run" />
    <target name="prepare">
        <mkdir dir="${dir.build}" />
        <delete>
            <fileset dir="${dir.build}" includes="**/*" />
        </delete>
    </target>
    <target name="compile">
        <javac classpathref="cp" destdir="${dir.build}" srcdir="${dir.src}" includes="**/*.java" includeantruntime="false" />
        <mkdir dir="${dir.lib}" />
        <jar destfile="${dir.lib}/AntExample.jar">
            <fileset dir="${dir.build}" includes="**/*.class" />
            <fileset dir=".">
                <include name="img/*.*" />
            </fileset>
        </jar>
    </target>
    <target name="run" if="test">
        <java classname="main.GeoAnalyzerMain" classpath="${dir.lib}/AntExample.jar">
            <arg line="${test}" />
        </java>
    </target>
</project>

现在将图像文件夹复制到结果存档中。但是JAR仍然无法执行。

编辑:

有效!

<?xml version="1.0" encoding="UTF-8" ?>
<project name="bsp0201" default="main" basedir=".">
    <property name="dir.src" value="./source" />
    <property name="dir.build" value="./classes" />
    <property name="dir.lib" value="./lib" />
    <path id="cp">
        <pathelement path="${classpath}" />
        <pathelement location="${dir.build}" />
    </path>
    <target name="main" depends="prepare, compile, run" />
    <target name="prepare">
        <mkdir dir="${dir.build}" />
        <delete>
            <fileset dir="${dir.build}" includes="**/*" />
        </delete>
    </target>
    <target name="compile">
        <javac classpathref="cp" destdir="${dir.build}" srcdir="${dir.src}" includes="**/*.java" includeantruntime="false" />
        <mkdir dir="${dir.lib}" />
        <jar destfile="${dir.lib}/AntExample.jar">
            <fileset dir="${dir.build}" includes="**/*.class" />
            <fileset dir=".">
                <include name="img/*.*" />
            </fileset>
            <manifest>
                <attribute name="Main-Class" value="main.GeoAnalyzerMain" />
            </manifest>
        </jar>
    </target>
    <target name="run" if="test">
        <java classname="main.GeoAnalyzerMain" classpath="${dir.lib}/AntExample.jar">
            <arg line="${test}" />
        </java>
    </target>
</project>