需要帮助为我的Java项目的Ant构建制作build.xml

时间:2012-01-02 20:49:27

标签: java ant build.xml

我有一个像这样设置的项目;

Project
    -src
        -com
            -top
                -classes
                    Class_3.java
                    Class_4.java
                -utils
                    Class_5.java
                    Class_6.java
                Class_1.java
                Class_2.java
    -data
        various files
    -libs
        lib_1.jar
        lib_2.jar
        lib_3.jar
        lib_4.jar
    build.xml

class_1.java包含主类,libs包含外部依赖项,数据包含各种图像,文本文件等...

我一直在尝试编写一个Ant构建文件来制作一个可分发的JAR文件,但我已经很难完成所有必需的库等...

这是我的目标。

<?xml version="1.0"?>
<project name="Project" default="jar">

    <property name="libsSrc" value="libs"/>
    <property name="build" value="build"/>
    <property name="classes" value="build/classes"/>
    <property name="jar" value="build/jar"/>
    <property name="libs" value="build/libs"/>

    <path id="classpath">
            <fileset dir="${libsSrc}" includes="*.jar"/>
    </path>

    <target name="clean" description="remove intermediate files">
        <delete dir="build"/>
    </target>

    <target name="compile" description="compile the Java source code to class files">
        <mkdir dir="${classes}"/>
        <javac srcdir="." destdir="${classes}" classpathref="classpath">
            <compilerarg line="-encoding utf-8"/>
        </javac>
    </target>

    <target name="jar" depends="compile" description="create a Jar file for the application">
        <mkdir dir="${jar}"/>
        <jar destfile="${jar}/App.jar">
            <fileset dir="${classes}" includes="**/*.class"/>
            <manifest>
                <attribute name="Main-Class" value="com.top.Class_1"/>
            </manifest>
        </jar>
    </target>

</project>

目前无效。

它编译时没有错误,但不包含所需的依赖项,并且JAR找不到主类。我该如何解决?

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0"?>
<project name="Rutherford" default="jar">

    <property name="libsSrc" value="libs"/>
    <property name="build" value="build"/>
    <property name="classes" value="build/classes"/>
    <property name="jar" value="build/jar"/>
    <property name="libs" value="build/libs"/>

    <path id="classpath">
            <fileset dir="${libsSrc}" includes="*.jar"/>
    </path>

    <pathconvert property="mf.classpath" pathsep=" ">
            <path refid="classpath"/>
            <mapper>
                    <chainedmapper>
                            <flattenmapper/>
                            <globmapper from="*.jar" to="lib/*.jar"/>
                    </chainedmapper>
            </mapper>
    </pathconvert>

    <target name="clean" description="remove intermediate files">
        <delete dir="build"/>
    </target>

    <target name="compile" description="compile the Java source code to class files">
        <mkdir dir="${classes}"/>
        <javac srcdir="." destdir="${classes}" classpathref="classpath">
            <compilerarg line="-encoding utf-8"/>
        </javac>
    </target>

    <target name="jar" depends="compile" description="create a Jar file for the application">
        <mkdir dir="${jar}"/>
        <jar destfile="${jar}/App.jar">
            <zipgroupfileset dir="${libsSrc}" includes="*.jar"/>
            <fileset dir="${classes}" includes="**/*.class"/>
            <manifest>
                <attribute name="Main-Class" value="com.top.Class_1"/>
                <attribute name="Class-Path" value="${mf.classpath}"/>
            </manifest>
        </jar>
    </target>

</project>

似乎可以做到这一点,但行;

<compilerarg line="-encoding utf-8"/>

似乎什么都不做,我有一些包含UTF-8编码字符的文本文件,这些字符被读入字符串并显示不正确显示; dσ/dΩ表示为dÃ/ d©。我会在another question中问这个问题。