Strange behavior of ant script that call another ant script, why?

I am very new to Ant (I came from Maven) and I am finding many problem to do the following operation.

I have a main project named CrystalIceGUI that use another dependency project named ShellExtBridge .

The dependency project ShellExtBridge have an own build.xml file that compile the project and pack it into a jar file putted into a direcotry named Release

The main project CrystalIceGUI have its build.xml ant file that contain a target named init that try to execute the build.xml file of the ShellExtBridge depency project to build this project and, later, use its jar in another target.

The problem is that if I execute the build.xml ant script of ShellExtBridge it work well and the jar file is correctly created into its Release directory (so I think that this ant script is ok) but if I call it from the build.xml of the main project it give me a long series of errors.

This is the build.xml of the ShellExtBridge dependency project:

<?xml version="1.0"?>
<project default="default">

    <!-- ============================================ -->
    <!-- Load build properties                        -->
    <!-- ============================================ -->

    <property name="project.buildfile" value="build.num" />
    <property file="${project.buildfile}" />
    <property file="info.properties" />


    <!-- ============================================ -->
    <!-- The default target                           -->
    <!-- ============================================ -->

    <target name="default" depends="jar" />

    <!-- Elimina le cartelle contenenti le classi compilate ed i jar -->
    <target name="clean">
        <echo message="Into ShellExtBridge build.xml clean target"/>
        <delete dir="../Release" />
        <!-- Elimina directory del jar finale -->
        <delete dir="bin" />
        <!-- Elimina directory delle classi compilate -->
    </target>


    <!-- Contiene i task di COMPILAZIONE: 
         1) Crea la directory "bin/" nel progetto
         2) Compila tutte le classi dentro la cartella "src" del progetto e mette i risultanti .class
            dentro "bin"
    -->
    <target name="compile" depends="clean">
        <echo message="Into ShellExtBridge build.xml compile target"/>
        <mkdir dir="bin" />
        <javac srcdir="src" destdir="bin" />
    </target>

    <target name="jar" description="Packs classes of shellextbridge" depends="compile">
        <echo message="Into ShellExtBridge build.xml jar target"/>
        <jar destfile="../Release/shellExtBridge.jar" index="false">
            <fileset dir="bin" />
            <manifest>
                <attribute name="Created-By" value="${info.software.author}" />
                <attribute name="Main-Class" value="com.techub.crystalice.rmi.Main" />
            </manifest>
        </jar>

    </target>

</project>

And this is the init target of the build.xml file of the main project CrystalIceGUI :

<target name="init">


        <ant antfile="${basedir}../../../ShellExtBridge/Project/build.xml" />
</target>

If I go to execute the init target of this ant script I obtain these errors:

Buildfile: /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/build.xml
init:
clean:
     [echo] Into ShellExtBridge build.xml clean target
   [delete] Deleting directory /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin
compile:
     [echo] Into ShellExtBridge build.xml compile target
    [mkdir] Created dir: /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin
    [javac] /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/ShellExtBridge/Project/build.xml:37: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 117 source files to /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin
    [javac] /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/src/com/techub/crystalice/utils/QuickLocale.java:8: package org.apache.log4j does not exist
    [javac] import org.apache.log4j.Logger;
    [javac]                        ^
    [javac] /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/src/com/techub/crystalice/fuse/AtmosFuseWrapper.java:8: package org.apache.log4j does not exist
    [javac] import org.apache.log4j.Logger;
    [javac]                        ^
    [javac] /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/src/com/techub/crystalice/fuse/AtmosFuseWrapper.java:11: package com.techub.crystalice.jaxb.settings does not exist
    [javac] import com.techub.crystalice.jaxb.settings.DriveType;
    [javac] 
...............................................................................
...............................................................................
ETCETCETC MANY OTHERS SIMILAR ERRORS
...............................................................................
...............................................................................
BUILD FAILED
/home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/build.xml:70: The following error occurred while executing this line:
/home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/ShellExtBridge/Project/build.xml:37: Compile failed; see the compiler error output for details.

As you can see seems that the build.xml ant script of the dependency ShellExtBridge project is called because the echo messages of this ant script are shown.

But there is something strange because when the build.xml script of the ShellExtBridge is called execute these operations:

[echo] Into ShellExtBridge build.xml clean target [delete] Deleting directory /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin compile: [echo] Into ShellExtBridge build.xml compile target [mkdir] Created dir: /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin [javac] /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/ShellExtBridge/Project/build.xml:37: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

It delete this directory /home/andrea/Documenti/XCloud/Implementazione/CrossPlatform/CrystalIceGUI/Project/bin instead the correct directory that is the one related to the bin directory inside the ShellExtBridge project.

Seems like that, when it call the ant file of the ShellExtBridge project, lost its references and do the operations on the folder of the main project and not of the right project.

Do you have some ideas about to solve it?


I think it is quite simple to fix. The <ant> task will use the current directory as working directory unless you specify the dir attribute (and additionally if the target file to run is named build.xml you can omit the antfile property).

So, here the init target that I suggest:

<target name="init">
    <ant dir="${basedir}../../../ShellExtBridge/Project" />
</target>

Here is the official doc about the dir property:

dir: the directory to use as a basedir for the new Ant project (unless useNativeBasedir is set to true). Defaults to the current project's basedir, unless inheritall has been set to false, in which case it doesn't have a default value. This will override the basedir setting of the called project. Also serves as the directory to resolve the antfile and output attribute's values (if any).


I could not get just the dir or the useNativeBasedir options to work on their own (using Ant 1.9.3). I had to supply inheritall="false" and specify the dir option for the sub-Ant build to use the correct basedir:

<target name="stage" depends="init">
    <ant antfile="../otherproject/build.xml" inheritall="false" dir="../otherproject">
</target>
链接地址: http://www.djcxy.com/p/44332.html

上一篇: 如何使用间距在蚂蚁脚本中设置相对路径?

下一篇: 调用另一个ant脚本的蚂蚁脚本的奇怪行为,为什么?