You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Taner Diler <ta...@riskturk.com> on 2005/08/22 13:28:57 UTC

get Property with java

Hi,

I try to get property that in defined in build.xml by using java. How 
can I do that?

build.xml

<...>
    <property name="property_1" value="1"/>
</...>

and java code

        Project ant = new Project();
        ProjectHelper helper = new ProjectHelperImpl();
        ant.init();
        helper.parse(ant, new File(xmlFile));
        String property_1 = ant.getProperty("property_1");

When I run this code, Null is returned by "ant.getProperty("property_1")".



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: get Property with java

Posted by Petar Tahchiev <pa...@gmail.com>.
On 22/08/05, Taner Diler <ta...@riskturk.com> wrote:
> 
> Hi,
> 
> I try to get property that in defined in build.xml by using java. How
> can I do that?
> 
> build.xml
> 
> <...>
> <property name="property_1" value="1"/>
> </...>
> 
> and java code
> 
> Project ant = new Project();
> ProjectHelper helper = new ProjectHelperImpl();
> ant.init();
> helper.parse(ant, new File(xmlFile));
> String property_1 = ant.getProperty("property_1");
> 
> When I run this code, Null is returned by "ant.getProperty("property_1")".
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> I have copy/pasted a source code from the ANT manual:

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)*;
        }
    }
}

I recommend you to go to the ant's site, then go to the manual link, 
Developing with Ant and the last tutorial is called "Tasks using Properties, 
Filesets & Patths" 
<http://ant.apache.org/manual/tutorial-tasks-filesets-properties.html>

 <http://ant.apache.org/manual/tutorial-tasks-filesets-properties.html>

-- 
Regards, Petar!