См. Разницу между , включая и import в руководстве по Ant Также используйте macrodefs .
Я немного адаптировал ваш пример, вам понадобятся несколько файлов:
build.xml, common.xml и macrodef_project_setup.xml
build.xml
<?xml version="1.0"?>
<project name="Ant_Test" basedir="." default="init">
<import file="common.xml"/>
<!-- This target overridden by the one in common.xml -->
<target name="common.init"/>
<target name="setup" depends="init"/>
<target name="do.several" depends="common.setup">
<echo message="In do.several, calling do.first" />
<antcall target="do.first" />
<echo message="In do.several, calling do.second" />
<antcall target="do.second" />
</target>
<target name="do.first" depends="common.setup">
<echo message="In do.first" />
</target>
<target name="do.second" depends="common.setup">
<echo message="In do.second" />
</target>
</project>
common.xml
<?xml version="1.0"?>
<project name="common">
<target name="init">
<project_setup option="Stack.Over.Flow"/>
</target>
<target name="setup">
</target>
<import file="macrodef_project_setup.xml"/>
</project>
macrodef
<?xml version="1.0"?>
<project name="project_setup" basedir=".">
<macrodef name="project_setup">
<attribute name="option" default=""/>
<sequential>
<!-- some process -->
<echo>THIS IS MY SETUP OPTION: @{option}</echo>
</sequential>
</macrodef>
</project>
Выход:
ant -p
Buildfile: build.xml
Main targets:
Other targets:
common.setup
do.first
do.second
do.several
init
setup
Default target: init
Целью по умолчанию теперь является init.
ant
Buildfile: build.xml
Ant_Test.init:
[echo] In setup initialization
[echo] THIS IS MY SETUP OPTION: Stack.Over.Flow
Но вы все равно можете использовать настройку муравья.
ant setup
Buildfile: build.xml
Ant_Test.init:
[echo] In setup initialization
[echo] THIS IS MY SETUP OPTION: Stack.Over.Flow
Запустите его с do.several.
ant do.several
Buildfile: build.xml
Ant_Test.init:
[echo] In setup initialization
[echo] THIS IS MY SETUP OPTION: Stack.Over.Flow
Ant_Test.do.several:
[echo] In do.several, calling do.first
Ant_Test.do.first:
[echo] In do.first
[echo] In do.several, calling do.second
Ant_Test.do.second:
[echo] In do.second