【问题标题】:Need help making build.xml for Ant build of my Java project需要帮助为我的 Java 项目的 Ant 构建制作 build.xml
【发布时间】:2012-01-04 00:48:29
【问题描述】:

我有一个这样的项目;

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 包含外部依赖项,data 包含各种图像、文本文件等...

我一直在尝试编写一个 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 找不到主类。我该如何解决?

【问题讨论】:

  • 如果您编辑的是您问题的答案,那么请将其作为答案发布(您可以稍后接受)。但实际上没有真正的问题,您可能想将其全部删除。
  • 抱歉,所有 cmets 已解决。

标签: java ant build.xml


【解决方案1】:
<?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 中询问。

【讨论】:

  • 我没听懂你的问题,你能运行你的 ant 任务吗???当你运行“ant compile”时会发生什么??
  • 我实际上已经解决了与这个问题相关的所有问题,我已经编辑(并且可能会编辑更多)原始问题以使其更加清晰。
猜你喜欢
  • 1970-01-01
  • 2011-08-26
  • 2023-02-03
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多