Pass variable to Java method from Ant Target

I currently have a .properties file for storing settings related to the framework. Example:

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

And I wrote a class to extract these values, and here is the method of this 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 a method similar to the one below to pinpoint the required property:

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

But I did not decide to do this and use Ant and pass the parameter instead of using it from the .properties file.

I was wondering how to pass the value to the Java method using Ant. None of these classes have core methods and will not have any core. Because of this, I could not use the properties of the java system.

+4
source share
3 answers

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

+1
source

If I understand your question correctly.

I believe using arguments will be easier. Whenever you run a java program

 java myJavaProgram [param1, param1, ...] 

As you can see, we can pass several parameters when starting a java program. Then all parameters will be saved in args.

The following is a program demonstrating how you can access the CLI in java.

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

Hope this helps.

0
source

Use ant as 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 a class from this using runtime arguments or you can make a package with a parameter already passed to the class and then call the package

0
source

Source: https://habr.com/ru/post/1310782/


All Articles