0

嗨,我的 ant build.xml 有问题,它可以工作,但有时不是第一次,或者在某些计算机上工作,而在其他计算机上不行

所以这里是:

<project name="My-java-api" default="dist-api" basedir=".">

<description>
    Java API Buildfile
</description>

<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist"  location="dist"/>
<property name="libs"  location="libs"/>

<!--if we don't remove folders, when we call compile-api
and classes have already been built, it doesn't build again-->

<target name="-init-api" depends="clean"
        description="Create folders libs and build">
    <mkdir dir="${build}"/>
    <mkdir dir="${libs}"/>
    <mkdir dir="${dist}"/>
</target>

 <!-- Here I call another buildfile of submodule located at the tree indicated I am 
 sure the other buildfile works perfect -->

<target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
    <ant dir="../Libraries/jelly/core/"
         antfile="build.xml"
         target="standalone-jar"/>
    <copy todir="${libs}">
        <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
    </copy>
</target>

 <!--so now I create this classpath to use for making jar-->

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

 <!--I compile source code including the jar that I have just copied to libs-->
<target name="compile-api" depends="-pre-build-api" >
    <javac srcdir="${src}"
           includeantruntime="false"
           destdir="${build}"
           classpathref="lib.classpath">
    </javac>
</target>
<!-- here i make jar with the classes and using the jar from external project,
I want just one jar for everything  -->

<target name="dist-api" depends="compile-api" >

    <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
        <zipgroupfileset dir="${libs}" includes="**/*.jar" />
    </jar>
</target>

<target name="clean"
        description="clean up" >
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
    <delete dir="${libs}"/>
</target>

编辑: failonerror="false" 添加到所有删除行

我不习惯蚂蚁,我一直在尝试和尝试,但它并不完全奏效。我希望我可以只使用命令行,但不能。奇怪的是,大多数时候如果运行它 2 次它就可以工作,但第一次真的很奇怪的工作人员发生了:要么不编译,要么类被重复。可能是什么原因?多谢

4

2 回答 2

3

你能解释为什么它可能会失败吗?快速浏览一下,我发现您的compile-api任务依赖于您的lib.classpath任务,但它不包含在compile-api的依赖项中。这可能会导致构建脚本有时工作而不是其他工作。

此外,lib.classpath依赖于正在创建的lib目录,因此它也应该依赖于-init-api

而且,你永远不应该依赖clean。Ant 构建已设置好,因此它们不必执行不必要的步骤。例如,如果您只更改一个源文件,则只有该源文件会被重新编译。您打破了每次构建完成时强制清理的构建脚本的整个想法。


仔细查看您的 build.xml,我意识到还有其他一些问题。

这是一个build.xml具有更正依赖关系的。

<project name="My-java-api" default="dist-api" basedir=".">

    <description>
        Java API Buildfile
    </description>

    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist"  location="dist"/>
    <property name="libs"  location="libs"/>

    <!--if we don't remove folders, when we call compile-api -->
    <!-- and classes have already been built, it doesn't build again-->

    <target name="-init-api"
        description="Create folders libs and build">
        <mkdir dir="${build}"/>
        <mkdir dir="${libs}"/>
        <mkdir dir="${dist}"/>
    </target>

    <!-- Here I call another buildfile of submodule located at the tree indicated I am -->
    <!--sure the other buildfile works perfect -->

    <target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
        <ant dir="../Libraries/jelly/core/"
            antfile="build.xml"
            target="standalone-jar"/>
        <copy todir="${libs}">
            <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
        </copy>
    </target>

    <!--so now I create this classpath to use for making jar-->

    <target name="lib.classpath"
        depends="-pre-build-api">
        <path id="lib.classpath"
            depends="-pre-build-api">
            <fileset dir="${libs}" includes="**/*.jar"/>
        </path>
    </target>

    <!--I compile source code including the jar that I have just copied to libs-->
    <target name="compile-api"
        depends="lib.classpath" >
        <javac srcdir="${src}"
            includeantruntime="false"
            destdir="${build}"
            classpathref="lib.classpath">
        </javac>
    </target>
    <!-- here i make jar with the classes and using the jar from external project,
    I want just one jar for everything  -->

    <target name="dist-api"
        depends="compile-api" >

        <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
            <zipgroupfileset dir="${libs}" includes="**/*.jar" />
        </jar>
    </target>

    <target name="clean"
        description="clean up" >
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        <delete dir="${libs}"/>
    </target>
</project>

注意dist-api 取决于 compile-api哪个取决于 lib.classpath哪个取决于 pre-build-api哪个_取决于-init.api。没有任何依赖clean

于 2013-07-10T19:00:23.007 回答
0

我认为如果目录不存在,它会抱怨,就像你第一次运行它时的情况一样。您可能希望将 failonerror="false" 属性添加到删除任务。

为了将来的参考,ant 已经不再用于构建 Java 了——大多数人已经转向 maven。

于 2013-07-10T16:18:27.007 回答