开发者

Pass Variable to Java Method from an Ant Target

At the moment I have a .properties file to store settings related to the framework. Example:

default.auth.url=http://someserver-at008:8080/
default.screenshots=false
default.dumpHTML=false

And I have written a class to extract those values and here is the method of that class.

public static String getResourceAsStream(String defaultProp) {
    String defaultPropValue = null;
    //String keys = null;
    try {
        InputStream inputStream = SeleniumDefaultProperties.class.getClassLoader().getResourceAsStream(PROP_FILE);
        Properties properties = new Properties();
        //load the input stream using properties.
        properties.load(inputStream);
        defaultPropValue = properties.getProperty(defaultProp);

    }catch (IOException e) {
        log.error("Something wrong with .properties file, check the location.", e);
    }
    return defaultPropValue;
}

Throughout the application I use method like the following to just exact the property needed:

public String getBrowserDefaultCommand() {
    String bcmd = SeleniumDefaultProperties.getResourceAsStream("default.browser.command");
    if(bcmd.equals(""))
        handleMissingConfigProperties(SeleniumDefaultProperties.getResourceAsStream("default.browser.command"));
    return bcmd;
}

But I have not decided do a change to this and use Ant and pass a parameter instead of using it from .properties file.

I was wond开发者_StackOverflowering how could I pass a value to a Java Method using Ant. None of these classes have main methods, and will not have any main. Due to this I was unable to use java system properties.


I think you will want to pass property values on the command line using the -Dpropname=propvalue syntax when you invoke java. See here.


If I understood your question right.

I suppose using args will be easier way. Whenever you run a java program

java myJavaProgram [param1, param1, ...]

As you see we can pass multiple parameters while starting java program. All the parameters will then be stored to args.

Below is the program demonstrating how you can access CLI in java.

public static void main(String[] args){
   for(String arg : args){
      System.out.println(arg);
   }

}

Hope this helps.


Use ant like below

<property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/>
<property name="file" location="ant/docs/manual/index.html"/>

<exec executable="${browser}" spawn="true">
    <arg value="${file}"/>
</exec>

you can directly call the class from this with runtime arguments or You can make batch with the parameter already passed to the class and then call the batch

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜