9

I have two ant files:

1) Main file

<include file="otherFile.xml" as="otherFile"/>

<target name="firstTarget">
    <antcall target="otherFile.secondTarget"/>
</target>

2) Utilities file

<target name="secondTarget">
    <antcall target="thirdTarget"/>
</target>

<target name="thirdTarget">
     <echo message="ok"/> 
</target>

When I invoke the firstTarget it says it cannot find the thirdTarget. If I change the secondTarget this way:

<target name="secondTarget">
    <antcall target="otherFile.thirdTarget"/>
</target>

then it works. But then I cannot use secondTarget directly. Because the second file does not knwon the prefix otherFile

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
user1518048
  • 451
  • 2
  • 5
  • 4

6 Answers6

4

You may try:

<ant antfile="otherFile.xml" target="secondTarget"/>

And no need to include the otherFile.

1

Use the following pattern in any file that will be both directly invoked and included:

<project name="my-project">
  <!-- if this is the top-level build.xml, ${self} == "" -->
  <condition property="self" value="">
    <equals arg1="${ant.project.name}" arg2="my-project" />
  </condition>
  <!-- if this is an included build.xml, ${self} == "my-project." -->
  <property name="self" value="my-project." /><!-- note the trailing dot -->
  ...

Then use the following for the antcall:

<antcall target="${self}target" />
Jay Dunning
  • 451
  • 6
  • 6
0

The ANT docs say that:

for every included file, Ant adds a property that contains the path to the included buildfile. With this path, the included buildfile can keep resources and be able to reference them relative to its position. So if I include for example a docsbuild.xml file named builddocs, I can get its path as ant.file.builddocs, similarly to the ant.file property of the main buildfile.

You can utilize this to do what you want i.e. make the antcall work whether it is being called from an 'included' context or the top-level context. What you should do is to set the value of a property say otherFile.context to 'otherFile.' if the property ant.file.otherFile is defined and '' (i.e. empty string) if its not. And then you can just use property expansion to invoke the target; for example:

<antcall target="${otherFile.context}thirdTarget"/>

Haven't tried it but I see no reason why it should not work.

Jawad
  • 193
  • 1
  • 8
0

Did you try it without fileName (just secondTarget ) :

<target name="firstTarget">
    <antcall target="secondTarget"/>
</target>

and import it without alias;

<include file="otherFile.xml"/>

Once, I did like this and it worked.

akdora
  • 893
  • 1
  • 9
  • 19
0
<import file="otherFile.xml"/>    
<target name="firstTarget">
        <antcall target="secondTarget"/>
</target>

use import task to upload your file, it's work for me!!!!

RCH
  • 3
  • 5
-1

You can make ant call for target which are in same file as below:

<target name="secondTarget">
    <antcall target="thirdTarget" antfile="${ant.file}"/>
</target>
Prerak Tiwari
  • 3,436
  • 4
  • 34
  • 64