You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Scot P. Floess" <sf...@nc.rr.com> on 2011/12/08 19:29:52 UTC
Creating a macrodef dynamically...within beanshell or any scripting
language?
All,
I have what may seem like a strange question... I was looking through the
source for Ant Contrib's ForTask.java... I see it uses MacroDef
internally as well as MacroInstance to execute().
I've got a script def that has a child element entitled "sequential" of
type org.apache.tools.ant.taskdefs.Sequential.
I was trying to mimick the ForTask within a Beanshell script. Below is
the code I was writing - but it definitely doesn't execute "sequential"
whatsoever. What started me on this was I liked how ForTask uses the same
param expansion that MacroDef uses...and wanted to see what ForTask did to
make that happen. I can definitely get "sequential" to execute by itself
within the for loop no problem... Here is the script def - and thanks
ahead of time!!!!!!!!
<scriptdef name="bshForLoop" language="beanshell">
<classpath>
<fileset dir="lib" includes="*.jar"/>
</classpath>
<attribute name="start"/>
<attribute name="end"/>
<attribute name="step"/>
<attribute name="param"/>
<element name="sequential"
classname="org.apache.tools.ant.taskdefs.Sequential"/>
<![CDATA[
try {
import org.apache.tools.ant.Task;
import org.apache.tools.ant.RuntimeConfigurable;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.taskdefs.MacroDef;
import org.apache.tools.ant.taskdefs.MacroInstance;
int start = Integer.parseInt(attributes.get("start"));
int end = Integer.parseInt(attributes.get("end"));
int step = Integer.parseInt(attributes.get("step"));
String param = attributes.get("param");
Task body = (Task) elements.get("sequential").get(0);
MacroDef macroDef = new MacroDef();
macroDef.setProject(project);
MacroDef.Attribute attribute = new MacroDef.Attribute();
attribute.setName(param);
attribute.setDefault("" + start);
macroDef.addConfiguredAttribute(attribute);
MacroDef.NestedSequential sequential =
macroDef.createSequential();
UnknownElement unknownElement = macroDef.getNestedTask();
unknownElement.setRealThing(body);
unknownElement.setProject(project);
sequential.addTask(unknownElement);
MacroInstance instance = new MacroInstance();
instance.setProject(project);
instance.setMacroDef(macroDef);
for (int index = start; index != end; index += step) {
instance.setDynamicAttribute(param, "" + index);
instance.execute();
}
}
catch(Exception e) {
e.printStackTrace();
}
]]>
</scriptdef
Scot P. Floess RHCT (Certificate Number 605010084735240)
Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Creating a macrodef dynamically...within beanshell or any
scripting language?
Posted by "Scot P. Floess" <sf...@nc.rr.com>.
I should mention that doing this will execute just fine as I expect:
<scriptdef name="bshForLoop" language="beanshell">
<classpath>
<fileset dir="lib" includes="*.jar"/>
</classpath>
<attribute name="start"/>
<attribute name="end"/>
<attribute name="step"/>
<element name="sequential"
classname="org.apache.tools.ant.taskdefs.Sequential"/>
<![CDATA[
try {
int start = Integer.parseInt(attributes.get("start"));
int end = Integer.parseInt(attributes.get("end"));
int step = Integer.parseInt(attributes.get("step"));
Task body = (Task) elements.get("sequential").get(0);
for (int index = start; index != end; index += step) {
body.execute();
}
}
catch(Exception e) {
e.printStackTrace();
}
]]>
</scriptdef>
I just loose that nice macrodef-ish @{param} expansion that I get in both
macrodef's and Ant Contrib for...
I'm completely open to not creating a macrodef on the fly if there is a
simple way to get that param expansion working easily :) I'm looking to
do something like this:
<bshForLoop start="1" end="10" param="foo">
<sequential>
<echo message = "Here we go index is @{foo}"/>
</sequential>
</bshForLoop>
On Thu, 8 Dec 2011, Scot P. Floess wrote:
> All,
>
> I have what may seem like a strange question... I was looking through the
> source for Ant Contrib's ForTask.java... I see it uses MacroDef internally
> as well as MacroInstance to execute().
>
> I've got a script def that has a child element entitled "sequential" of type
> org.apache.tools.ant.taskdefs.Sequential.
>
> I was trying to mimick the ForTask within a Beanshell script. Below is the
> code I was writing - but it definitely doesn't execute "sequential"
> whatsoever. What started me on this was I liked how ForTask uses the same
> param expansion that MacroDef uses...and wanted to see what ForTask did to
> make that happen. I can definitely get "sequential" to execute by itself
> within the for loop no problem... Here is the script def - and thanks ahead
> of time!!!!!!!!
>
> <scriptdef name="bshForLoop" language="beanshell">
> <classpath>
> <fileset dir="lib" includes="*.jar"/>
> </classpath>
>
> <attribute name="start"/>
> <attribute name="end"/>
> <attribute name="step"/>
> <attribute name="param"/>
>
> <element name="sequential"
> classname="org.apache.tools.ant.taskdefs.Sequential"/>
>
> <![CDATA[
> try {
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.RuntimeConfigurable;
> import org.apache.tools.ant.UnknownElement;
>
> import org.apache.tools.ant.taskdefs.MacroDef;
> import org.apache.tools.ant.taskdefs.MacroInstance;
>
> int start = Integer.parseInt(attributes.get("start"));
> int end = Integer.parseInt(attributes.get("end"));
> int step = Integer.parseInt(attributes.get("step"));
> String param = attributes.get("param");
> Task body = (Task) elements.get("sequential").get(0);
>
> MacroDef macroDef = new MacroDef();
> macroDef.setProject(project);
>
> MacroDef.Attribute attribute = new MacroDef.Attribute();
> attribute.setName(param);
> attribute.setDefault("" + start);
> macroDef.addConfiguredAttribute(attribute);
>
> MacroDef.NestedSequential sequential =
> macroDef.createSequential();
>
> UnknownElement unknownElement = macroDef.getNestedTask();
> unknownElement.setRealThing(body);
> unknownElement.setProject(project);
>
> sequential.addTask(unknownElement);
>
> MacroInstance instance = new MacroInstance();
> instance.setProject(project);
> instance.setMacroDef(macroDef);
>
> for (int index = start; index != end; index += step) {
> instance.setDynamicAttribute(param, "" + index);
>
> instance.execute();
> }
> }
>
> catch(Exception e) {
> e.printStackTrace();
> }
> ]]>
> </scriptdef
>
> Scot P. Floess RHCT (Certificate Number 605010084735240)
> Chief Architect FlossWare http://sourceforge.net/projects/flossware
> http://flossware.sourceforge.net
> https://github.com/organizations/FlossWare
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
Scot P. Floess RHCT (Certificate Number 605010084735240)
Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org