【问题标题】:How to access the command line args passed from Maven in the Java program [duplicate]如何在Java程序中访问从Maven传递的命令行参数[重复]
【发布时间】:2014-02-22 09:35:33
【问题描述】:

在 Maven 中创建一个测试项目。借助来自论坛/教程的信息,我已经能够创建以下 pom。我的要求是在 Java 方法中读取命令行参数。我找到了有关“exec-maven-plugin”的信息,但无法获得有关如何在 Java 代码中访问这些参数的信息?

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>TestProject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>TestProject</name>
<url>http://maven.apache.org</url>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.test.driver.TestDriver</mainClass>
                <arguments>
                    <argument>timestamp=023012</argument>
                    <argument>currentdate=01292014</argument>
                </arguments>
            </configuration>
        </plugin>

    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium.server</groupId>
        <artifactId>selenium-server</artifactId>
        <version>1.0.3-standalone</version>
    </dependency>
</dependencies>
 </project>

【问题讨论】:

  • 在 Java 代码中,您必须分析 public static void main() 方法的 args 参数。
  • “分析”是什么意思?我正在寻找代码语法以在程序中读取它们?还有代码应该在命令行中提到什么?
  • @BrianRoach - 这没有回答我的第二部分。如何在 Java 程序中访问这些 args?
  • @BrianRoach - 这对我不起作用,因为我使用的是 TestNG 而不是调用 main 方法。在回答或标记重复问题之前,您应该正确阅读问题。

标签: java maven selenium


【解决方案1】:

没有简单的方法可以访问命令行参数。通常的解决方法是改用System properties

    <configuration>
      <mainClass>com.example.Main</mainClass>
      <systemProperties>
        <systemProperty>
          <key>timestamp</key>
          <value>023012</value>
        </systemProperty>
        <systemProperty>
          <key>currentdate</key>
          <value>01292014</value>
        </systemProperty>
        ...
      </systemProperties>
    </configuration>

您可以使用System.getProperties()在任何地方访问这些内容

相关文章:

【讨论】:

  • 试过你的答案,但 System.getProperty("timestamp") 给出 null....
  • 对不起,您需要使用&lt;key&gt;&lt;value&gt; 元素,而不是使用系统属性名称作为元素名称。查看我的编辑。
  • 还是不行:(感谢文章。
  • 尝试用-X运行mvn;然后 exec 插件应该向您展示它的真正作用。
猜你喜欢
  • 1970-01-01
  • 2015-06-05
  • 2012-04-23
  • 1970-01-01
  • 2020-03-02
  • 2018-03-28
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多