I don't think there are any Ant built-in tasks that do what you want. However, you can tip over using the extensible nature of Ant.
I hacked a really (and really mean really ) dirty example that you can use as a spring board for the right task.
package q1015732;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class BindPropertyTask extends Task{
private String _classFile = null;
private String _fieldName = null;
private String _propName = null;
public void setFieldName(String fieldName){
_fieldName = fieldName;
}
public void setClassFile(String classFile){
_classFile = classFile;
}
public void setProperty(String propName)
{
_propName = propName;
}
public void execute() throws BuildException{
if(_classFile == null) throw new BuildException("ClassFile is a required attribute");
if(_fieldName == null) throw new BuildException("FieldName is a required attribute");
if(_propName == null) throw new BuildException("Property is required attribute");
CustomLoader loader = new CustomLoader();
Class toInspect = null;
Field toBind = null;
Object value = null;
try {
toInspect = loader.loadClass(new FileInputStream(_classFile));
} catch (Exception e) {
throw new BuildException("Couldn't load class ["+e.getMessage()+"], in ["+(new File(_classFile).getAbsolutePath())+"]");
}
try{
toBind = toInspect.getField(_fieldName);
}catch(NoSuchFieldException e){
throw new BuildException("Couldn't find field, '"+_fieldName+"'");
}
toBind.setAccessible(true);
try{
value = toBind.get(null);
}catch(NullPointerException e){
throw new BuildException("Field is not static");
} catch (Exception e) {
throw new BuildException("Unable to access field ["+e.getMessage()+"]");
}
if(value != null)
this.getProject().setProperty(_propName, value.toString());
else
this.getProject().setProperty(_propName, null);
}
class CustomLoader extends ClassLoader{
public CustomLoader(){
super(ClassLoader.getSystemClassLoader());
}
@SuppressWarnings("deprecation")
public Class loadClass(InputStream in) throws Exception{
byte[] classData = loadData(in);
return this.defineClass(classData, 0, classData.length);
}
private byte[] loadData(InputStream in) throws Exception{
byte[] buffer = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int i;
while((i = in.read(buffer)) != -1){
out.write(buffer, 0, i);
}
return out.toByteArray();
}
}
}
An example assembly file using this task:
<project name="q1015732">
<target name="test">
<taskdef name="static_bind" classname="q1015732.BindPropertyTask" />
<static_bind fieldname="StringBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="string.value" />
<static_bind fieldname="IntBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="int.value" />
<static_bind fieldname="DoubleBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="double.value" />
<echo message="StringBind: ${string.value}" />
<echo message="IntBind: ${int.value}" />
<echo message="DoubleBind: ${double.value}" />
</target>
</project>
DummyMain.java:
package q1015732.test;
public class DummyMain {
public static String StringBind = "I'm a String!";
public static int IntBind = 1024;
public static double DoubleBind = 3.14159;
}
The result of the build using the Ant file:
Test:
[echo] StringBind: I'm a string!
[echo] IntBind: 1024
[echo] DoubleBind: 3.14159
BUILDING SUCCESSFULLY
: , , . , , , .