【发布时间】: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 方法。在回答或标记重复问题之前,您应该正确阅读问题。