0

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!

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

1

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?

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
0

You can use the following idiom to determine automatically whether a particular build file is the main file called by the user or whether it has been imported:

<project name="projectA">
  <!-- set a property if this file is standalone, don't set it if imported -->
  <condition property="projectA.standalone">
    <equals arg1="${ant.file}" arg2="${ant.file.projectA}" />
  </condition>

  <target name="standalone-only" if="projectA.standalone">
    <echo>I am standalone</echo>
  </target>

  <target name="imported-only" unless="projectA.standalone">
    <echo>I have been imported</echo>
  </target>
</project>

The trick here is that the implicit property ant.file is set to the path of the main build file that was specified on the command line (or used implicitly if it's called build.xml), but in addition Ant sets properties ant.file.PROJECTNAME to the path of the relevant build file for every (main or imported) <project name="PROJECTNAME">. Thus ${ant.file} == ${ant.file.PROJECTNAME} if and only if PROJECTNAME is the top-level build file.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183