Ant:Ant如何找到属性的属性setter方法

时间:2016-10-25 10:13:30

标签: java ant

我在这里引用示例:https://ant.apache.org/manual/tutorial-tasks-filesets-properties.html

import org.apache.tools.ant.BuildException;

public class Find extends Task {

    private String property;
    private String value;
    private String print;

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

    // setter for value and print

    public void execute() {
        if (print != null) {
            String propValue = getProject().getProperty(print);
            log(propValue);
        } else {
            if (property == null) throw new BuildException("property not set");
            if (value    == null) throw new BuildException("value not set");
            getProject().setNewProperty(property, value);
        }
    }
}

该示例扩展了Ant任务以构建自定义任务。给出的ant任务脚本

   <find property="test" value="test-value"/>
   <find print="test"/>

脚本正在设置属性“property”和“print”的值。我的问题是Ant如何确定它必须调用“setProperty”方法来设置“property”属性的值?基本上Ant如何确定从类中调用哪个方法?

1 个答案:

答案 0 :(得分:0)

来自Writing Your Own Task

  

对于每个属性,编写一个setter方法。 setter方法必须是一个带有单个参数的public void方法。 方法的名称必须以set开头,后跟属性名称,名称的第一个字符为大写,其余为小写*。也就是说,要支持名为file的属性,请创建方法setFile。

来自The Life-cycle of a Task

  
      
  1. ...
  2.   
  3. 此任务的所有属性都在运行时通过相应的setXXX方法设置。
  4.   
  5. ...
  6.   
相关问题