Search code examples
javaantant-contrib

Ant: set property in 1 file, read it in another?


I have the following Ant buildfile importer.xml:

<project name="importer" basedir=".." default="build">
    <import file="imported.xml"/>

    <target name="build">
        <!-- Do some stuff... -->

        <property name="isRunningFromImporter" value="true"/>
        <antcall target="run-now"/>
    </target>
</project>

And another buildfile imported.xml that uses the ant-contrib tasks:

<project name="importer" basedir=".." default="build">
    <!-- Most of file omitted for brevity -->

    <target name="run-now">
        <if>
            <not-equals arg1="${isRunningFromImporter}" arg2="true"/>
            <then>
                <!--
                    This should only execute when the
                    isRunningFromImporter property is not true.
                -->
            </then>
        </if>
    </target>
</project>

The imported#run-now target can be ran as a standalone Ant task, for example:

ant -buildfile imported.xml run-now

In this case I do not want the <then> clause/task executed. However, if you run the same task as it is imported into importer.xml:

ant -buildfile importer.xml build

Then I want the <then> clause/task to execute, however, Ant does no allow me to see a property in one file and read it in another. Any ideas? Thanks in advance!


Solution

  • It does what you want by default. Antcall's "inheritAll" attribute is set to true.

    Running the following code echo's "true" showing the property is in fact set.

    <project name="importer" basedir=".." default="build">
        <import file="imported.xml"/>
    
        <target name="build">
            <!-- Do some stuff... -->
    
            <property name="isRunningFromImporter" value="true"/>
            <antcall target="run-now"/>
        </target>
    </project>
    
    <project name="importer" basedir="..">
        <!-- Most of file omitted for brevity -->
    
        <target name="run-now">
    
            <echo>${isRunningFromImporter}</echo>
        </target>
    </project>
    

    I'm not familiar with <not-equals arg1="${isRunningFromImporter}" arg2="true"/>. I've always used <not><equals ...> instead. Where does not-equals come from? Are you sure the problem isn't in that line?