You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Michael Sunde <ms...@actional.com> on 2004/01/17 01:30:06 UTC
RE: is it possible to create targets dynamically? - Found Solution
Hi,
It took a little while, but I found a way of creating targets dynamically using javascript.
Benefits of this approach:
- can call the dynamic ant target from the command line like a standard target. No need to set an extra property.
- the target dependencies seems to work. A static target can depend on a dynamically created target.
scriptdef is impressive :>
Thanks,
Michael
====================================
build.xml
====================================
<project name="test" default="all" basedir=".">
<!-- create a new target -->
<scriptdef name="createTarget" language="javascript">
<attribute name="name"/>
<element name="body" type="sequential"/>
<![CDATA[
function newInstance(className)
{
return java.lang.Class.forName(className).newInstance();
}
var target = newInstance("org.apache.tools.ant.Target");
target.setProject(project);
target.setName(attributes.get("name"));
target.setDescription("Dynamic target named: " + attributes.get("name"));
target.addTask(elements.get("body").get(0));
project.addTarget(target);
]]>
</scriptdef>
<!-- macro that creates a target -->
<macrodef name="echoTarget">
<attribute name="name" />
<sequential>
<createTarget name="@{name}">
<body>
<echo>@{name} target executed.</echo>
<!-- the real work goes here. -->
</body>
</createTarget>
</sequential>
</macrodef>
<!-- multiple dynamic targets -->
<echoTarget name="dynamic1"/>
<echoTarget name="dynamic2"/>
<echoTarget name="dynamic3"/>
<echoTarget name="dynamic4"/>
<!-- depend on a dynamic target -->
<target name="all" depends="dynamic1, dynamic2"/>
</project>
====================================
Output of Calling: ant
====================================
Buildfile: build.xml
dynamic1:
[echo] dynamic1 target executed.
dynamic2:
[echo] dynamic2 target executed.
all:
BUILD SUCCESSFUL
Total time: 2 seconds
====================================
Output of Calling: ant dynamic3
====================================
Buildfile: build.xml
dynamic3:
[echo] dynamic3 target executed.
BUILD SUCCESSFUL
Total time: 2 seconds
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org