如何访问Java程序中Maven传递的命令行ARGS



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

没有简单的方法来访问命令行参数。通常的解决方法是使用系统属性:

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

在任何地方访问这些

相关文章:

  • http://mojo.codehaus.org/exec-maven-plugin/usage.html上的" Java目标"

最新更新