ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scretti <>
Subject new element suggestion - java rather than script
Date Wed, 02 May 2007 17:37:41 GMT

I'm new to contributing on an open source project but have used ant for many
years so 
forgive me if this topic has been discussed previously.  I searched the dev
list but 
did not see anything quite like what I am about to suggest.  I have a
working task 
implemented as described in the Usage Examples below and could post it to a
right away if it gets blessed.  However, it could probably use a bit more
testing before it is ready for prime-time.  

The reason that I suggest it for the core is that I would need to either
include it 
in each project that it is used in or ensure that all developers install the
task in their ant environment.  Not that either of these options are
terrible, but I've
also wished for this functionality for many years and only recently
implemented it.

Thanks in advance for your input.

Background :

Ant does not have "normal" programming language constructs such as
loops, conditions(it has them but they can be difficult), math, etc.

The ability exists to add on optional scripting languages, but that is not
part of a "default" ant install.  There is talk of incorporating a default
language in 1.7.1 which would be great, but this solution may make that

There is also the ability to extend ant and create your own tasks.  That
fine, but overkill for simple things especially if the functionality already
exists in the java language(such as Math.XXX(...)).  Again, not part of a
default ant
install.  This is limiting in an environment where developers have freedom
configure their environment as they wish.  It is much easier to require a
version of ant rather than a minimum version with the addition of X, Y, Z


The suggested task would be used to execute any java class method or even to
and execute a java class defined in-line of the XML element on the fly. 
This would 
give the power of the full Java API to ant build files.  
Suggested element would be something like : 

This suggestion leverages known features of the ant runtime : 
		Have access to jdk and compiler (should have a javac if ant is used to
compile java)
		Already running in Java VM
		Dynamic classloading

Usage examples :

To invoke the method, an instance of the class is first instantiated using
the default constructor.
This instance is passed to the invoke method of the Method class.  
If an instance of the class cannot be created, invoke is called passing null
for the instance 
and attempting to invoke the method as if it were static.
	<taskdef name="jexecutor" classname=""
classpath="${basedir}/classes" />
	Example 1 - calling static no-arg method of existing class -->

	    <jexecutor classname="java.lang.Math" methodname="random"
outputproperty="output1" />
	    <echo>Result of call : ${output1}</echo>

	Example 2 - calling paramed method where runtime type differs from method
signiture type.
	 		Note : signature is :
javax.swing.JOptionPane.showMessageDialog(Component, Object)
	 		Since we're passing in a String as the second argument, we want it
	 		and used as a string...hence runtype="string".  However, since the
method signiture
	 		is defined with a Component and Object, searching for it with reflection
	 		Component and String will not find it.  Therefore we also need
	 		so that the correct method can be found.

	<jexecutor classname="javax.swing.JOptionPane"
methodname="showMessageDialog" >
		<metharg value="" sigtype="java.awt.Component" />
		<metharg value="a messgage" sigtype="java.lang.Object" runtype="string" />

	Example 3 - Defining class inline and executing a method 
	        Note : some characters may require the inline class definition
below to
	               be defined in a CDATA block but for this simple case it
works fine
	<jexecutor methodname="testMethod" outputproperty="testout" >
		public class TestClass
			public String testMethod()
				return "non-static Blah";

View this message in context:
Sent from the Ant - Dev mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message