The role of the antcall task is to allow other targets to be called and executed during the execution of a target. For example, if you need to compile the project before packaging the project, you can execute the compiled target first through the antcall task in the target of the packaging project. In this case, you can also set the depends attribute between targets. The antcall task must be executed within the target element. This task cannot be executed outside the target element.
4.2.1 antcall task attributes and functions
The antcall task includes three attributes: Target, inheritall, and inheritrefs. The details are as follows:
(1) Target attribute: In an antcall task, the target attribute is used to specify the target to be called for execution. It is required to specify this target attribute by name. It is worth noting that when the target called by the antcall task has a dependent target (the target is specified in depends), the specified target in the depends attribute will also be executed.
(2) inheritall attribute: used to specify whether to inherit the current attribute. The default value is true, which indicates that the called target can use these attributes.
(3) inheritrefs attribute: used to specify whether to overwrite the reference attribute or whether to create a reference to the current reference attribute. By default, the antcall task does not overwrite the reference attribute unless the inheritrefs attribute is set to true. By default, the inheritrefs attribute is false.
4.2.2 use antcall task to implement instances called between targets
The antcall task is used to call each other between targets. The following is a compilation of the component File antcallsample. XML to call and execute targetb in targeta. The component file is as follows:
<? XML version = "1.0"?>
<Project name = "antcallsample" default = "targeta">
<Target name = "targeta">
<Echo message = "in targeta calling targetb"/>
<! -- // Call targetb -->
<Antcall target = "targetb">
</Antcall>
<Echo message = "after call targetb"/>
</Target>
<Target name = "targetb">
<Echo message = "in targetb"/>
</Target>
</Project>
Save the component file and run the ant-F antcallsample. xml command on the command line. The execution result is 4.3.
Figure 4.3 antcallsample. xml
The above example is used to execute targetb during the execution of targeta. If the depends attribute is set in targetb, the target specified in the depends attribute is also executed. Modify antcallsample. XML as follows to demonstrate the dependency of depends:
<? XML version = "1.0"?>
<Project name = "antcallsample" default = "targeta">
<Target name = "init">
<Echo message = "in init target"/>
</Target>
<Target name = "targeta">
<Echo message = "in targeta calling targetb"/>
<Antcall target = "targetb">
</Antcall>
<Echo message = "after call targetb"/>
</Target>
<Target name = "targetb" depends = "init">
<Echo message = "in targetb"/>
</Target>
</Project>
Save the component file and run the ant-F antcallsample. xml command again. The execution result is 4.4.
Figure 4.4 antcallsample. xml command execution result
The preceding execution result indicates that targetb is called when targeta is executed, because targetb depends on init target. Therefore, init target is executed before targetb.
4.2.3 use antcall task to implement instances that pass parameters between target calls
You can use the <param> type to transmit parameters from a target to the called target. Of course, you can also define the property in the target to implement it, which is similar to the parameter passed in the Java method call. Modify antcallsample. XML to implement the parameter passing function. The content is as follows:
<? XML version = "1.0"?>
<Project name = "antcallsample" default = "targeta">
<Target name = "init">
<Echo message = "in init target"/>
</Target>
<Target name = "targeta">
<Echo message = "in targeta calling targetb"/>
<! -- // Pass through property -->
<Property name = "prop" value = "prop property"/>
<Antcall target = "targetb">
<! -- // Set param through antcall -->
<Param name = "path1" value = "path1 Param"/>
</Antcall>
<Echo message = "after call targetb"/>
</Target>
<Target name = "targetb" depends = "init">
<Echo message = "in targetb"/>
<Echo message = "path1 =$ {path1}"/>
<Echo message = "prop =$ {prop}"/>
</Target>
</Project>
Run the ant-F antcallsample. xml command again. The execution result is 4.5.
Figure 4.5 antcallsample. xml
The execution result shows that the attributes specified by property And Param in antcall are passed to targetb. For the param type, there are only two attributes: Name and value. Because the default value of the inheritall attribute in the antcall task is true, the property can be referenced by targetb. If the same property is also defined in targetb, you can set the inheritrefs attribute and reference type to overwrite it.