如何从自定义的Ant task中返回值

在自定义的task中如果想将值返回给Ant,需要在自定义的task中通过getProject().setNewProperty(name,value) 方法将property名称和值关联起来.同时需要用public void setProperty(String propertyName) 得到property名称.

实例:

JAVA 类
public class OwnTask extends Task {
private String prop1;
private String propertyName;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

public void setProperty(String propertyName) {
this.propertyName = propertyName;
}

public void execute() throws BuildException {
System.out.println("Execute Customized task " + prop1);
setProp1(prop1 + " Changed");
// prop1 = prop1 + " Changed";
System.out.println("Execute Customized task " + prop1);

getProject().setNewProperty(this.propertyName, "test change value in own task");
}

}
build xml文件:

<?xml version="1.0"?>

<project name="TestTask" basedir=".">

<property name="mainArg" value="original value" />
<property name="propArg" value="original value" />
<property name="project.lib.path" value="D:/temp" />

<path id="testTask.classpath">
<fileset dir="${project.lib.path}">
<include name="**/*.jar" />
</fileset>

</path>

<taskdef name="ownTask" classname="com.test.owntask.OwnTask" classpathref="testTask.classpath" />

<target name="testTask">
<ownTask property="testProp" prop1="${propArg}">
</ownTask>
<echo message="------------------- ${testProp} -------------------------------------" />
<echo message="------------------- ${propArg} -------------------------------------" />
</target>

<target name="testMain">

<echo message="------------------- ${project.lib.path} -------------------------------------" />
<java classname="com.test.owntask.MainTest">
<arg value="${mainArg}" />
<classpath>
<fileset dir="${project.lib.path}">
<include name="**/*.jar" />
</fileset>

</classpath>

</java>
<echo message="------------------- ${mainArg} -------------------------------------" />
</target>


</project>
在上面的例子<ownTask property="testProp" prop1="${propArg}">中,testProp是property,在JAVA类中改变后值可以传回Ant.

prop1是通过set方法传入的信息,值不会被传回Ant.同样,上面的例子中JAVA main方法改变的值也不会传回Ant.

你可能感兴趣的:(task)