0

我已经编写/复制了一个简单的 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>

我的蚂蚁突击队和结果输出:

# 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 生成它时)并且我无法执行它。我做错了什么?

谢谢

4

2 回答 2

0

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

JAR 比较

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

于 2013-02-09T12:13:40.770 回答
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>
于 2013-02-11T09:40:31.667 回答