If you need to execute another ant project by using the ant command in a component file, or you want to execute the component file of the subitem of the project. Now ant is a core task. Simply put, the ant task is equivalent to executing the component file through the ant command in the command line. It is worth noting that the ant task depends on the target element and needs to be used in a target element. It cannot be used independently outside the target element.
Ant tasks can also implement inheritance relationships similar to Java programs. However, for this core task, only the property element and reference attribute are inherited. For example, project a contains Project B executed through the ant task, and Project B needs to use the attribute C in Project, in this case, you can set the inheritall attribute in the ant task so that Project B can use the attribute C in Project. On the other hand, this is also in line with the idea of subdivided functions in XP (extreme) programming. You can divide large projects into small projects and integrate them using ant tools.
4.1.1 attributes and functions of ant task
Ant tasks include attributes such as antfile, Dir, target, output, inheritall, and inheritrefs, which are described as follows:
(1) antfile attribute: In an ant task, the antfile attribute specifies the name of the component file to be executed and can contain path information. For example, execute the projectb component file in projecta. The content of the projecta component file is as follows:
<? XML version = "1.0"?>
<! -- Execute the projectb component file in projecta -->
<Project name = "porjecta" default = "callprojectb">
<Target name = "callprojectb">
<Echo message = "in projecta calling projectb"/>
<Ant antfile = "subfile/projectb. xml"/>
</Target>
</Project>
Project TB is stored in the subfile directory. The specific content is as follows:
Hamg <? XML version = "1.0"?>
<Project name = "porjectb" default = "init">
<Target name = "init">
<Echo message = "in projectb"/>
</Target>
</Project>
If you want to run the project TB component file through projecta, you can run ant-buildfile projecta. XML in the command line. The execution result is 4.1.
Figure 4.1 run projectb through projecta
According to the execution results, the role of the antfile attribute is equivalent to specifying the-buildfile subfile/projectb. xml option in the ant command, except that the ant task provides more convenient functions. If the antfile attribute is not set, the ant task searches for the component file named build. xml. Of course, the antfile attribute can also be used with the Dir attribute. antfile specifies the file name and the Dir attribute specifies the directory where the file is located.
(2) dir attribute: In the ant task, the Dir attribute is used to specify the directory where the component file is located. It is equivalent to the basedir attribute of the component file to be executed. By default, if the Dir attribute is not set, the root directory of the currently executed component file is used as the root directory of the ant task. After this attribute is set, the basedir attribute of the component file to be executed takes the value of the Dir attribute. The following describes how to rewrite the targeta. xml component file to specify a directory through the Dir attribute to implement the same function. The specific component file content is as follows:
<? XML version = "1.0"?>
<Project name = "porjecta" default = "callprojectb">
<! -- // Call the projectb. xml component file -->
<Target name = "callprojectb">
<Echo message = "in projecta calling projectb"/>
<Ant antfile = "projectb. xml" dir = "subfile"/>
</Target>
</Project>
(3) Target attribute: In an ant task, the target attribute is used to specify the target to be executed in the component file to be executed. If the target attribute is not set, the default target (that is, the default value specified in the project element) in the called component file will be executed ). This attribute is equivalent to specifying the target option to be executed during command line execution. For example, call projectb1 in projecta1 and execute target "TARGET2" in projectb1 ". Compile the projecta1.xml component file with the following content:
<? XML version = "1.0"?>
<Project name = "porjecta" default = "callprojectb">
<Target name = "callprojectb">
<Echo message = "in projecta calling projectb"/>
<! -- // Execute the projectb. xml component file through the ant task, and specify to execute the target of TARGET2 -->
<Ant antfile = "subfile/projectb. xml" target = "TARGET2"/>
</Target>
</Project>
The content of the called projectb. xml component file is as follows:
<? XML version = "1.0"?>
<Project name = "porjectb" default = "init">
<Target name = "init">
<Echo message = "in projectb"/>
</Target>
<Target name = "TARGET2">
<Echo message = "in projectb, execute TARGET2"/>
</Target>
</Project>
Then, execute ant-F projecta1.xml in the command line to view the execution result of the ant task, as shown in Result 4.2.
Figure 4.2 project call results
The execution result shows that the TARGET2 target in targetb. XML is executed through the ant task in targeta. xml.
(4) Output attribute: In the ant task, the output attribute is mainly used to specify the information output when the ant tool is executed. It can be located on the console or in a file. When the output information is in a file, it is equivalent to specifying the-logfile <File>,-l <File> option on the command line. If the Dir attribute is specified, the location of the specified output file is relative to the directory specified by the Dir attribute. Of course, you can also specify it through the absolute path. For example, locate the output information in projecta to the out. log file. The component file content is as follows:
<? XML version = "1.0"?>
<Project name = "porjecta" default = "callprojectb">
<Target name = "callprojectb">
<Echo message = "in projecta calling projectb"/>
<! -- // Output information to out. log -->
<Ant antfile = "subfile/projectb. xml" output = "Out. log"/>
</Target>
</Project>
(5) inheritall attribute: In the ant task, the inheritall attribute is used to specify the properties in the current project element of the called component file. This property is similar to the inheritance relationship of classes in Java programs. By default, the value of inheritall is true.
(6) inheritrefs attribute: If the inheritrefs attribute is set to true in the ant task, the project element in the called component file can reference the reference task in the current project element.
The reference task is used to copy the current attribute to the called ant project. The references task has two attributes:
● RefID attribute: This attribute represents the property ID of the current project.
● Torefid attribute: used to specify the reference ID in the called Project.
Example of reference:
<Reference refID = "path1" torefid = "path2"/>
<! -- // Pass the path1 attribute of the current project to the called Project for use,
Reference this attribute in the called Project through path2. -->