如何访问Java程序中从Maven传递的命令行参数

时间:2014-01-30 07:40:22

标签: java maven selenium

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

1 个答案:

答案 0 :(得分:0)

没有简单的方法来访问命令行参数。通常的解决方法是使用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()

在任何地方访问这些内容

相关文章: