【问题标题】:Pass Variable to Java Method from an Ant Target将变量从 Ant 目标传递给 Java 方法
【发布时间】:2011-02-23 09:54:47
【问题描述】:

目前我有一个 .properties 文件来存储与框架相关的设置。 示例:

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

我已经编写了一个类来提取这些值,这是该类的方法。

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

在整个应用程序中,我使用如下方法来精确确定所需的属性:

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

但我还没有决定对此进行更改并使用 Ant 并传递参数而不是从 .properties 文件中使用它。

我想知道如何使用 Ant 将值传递给 Java 方法。这些类都没有主要方法,也没有任何主要方法。因此,我无法使用 java 系统属性。

【问题讨论】:

    标签: java ant build properties


    【解决方案1】:

    我认为您在调用 java.util 时会希望使用-Dpropname=propvalue 语法在命令行上传递属性值。见here

    【讨论】:

    • 我在提交问题之前通过 Google 搜索找到了这个网站,但它没有用。
    【解决方案2】:

    如果我理解你的问题是正确的。

    我想使用 args 会更简单。每当你运行一个java程序时

    java myJavaProgram [param1, param1, ...]
    

    如您所见,我们可以在启动 java 程序时传递多个参数。然后将所有参数存储到 args。

    以下是演示如何在 java 中访问 CLI 的程序。

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      像下面这样使用蚂蚁

      <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>
      

      你可以使用运行时参数直接调用这个类,或者你可以使用已经传递给类的参数进行批处理,然后调用批处理

      【讨论】:

        猜你喜欢
        • 2012-02-09
        • 2012-08-21
        • 1970-01-01
        • 2011-12-12
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多