-1

Creating a test project in Maven. With information from forums/tutorials, i have been able to create the below pom. My requirement is to read command line arguments in a Java method. I found info on the "exec-maven-plugin", but could not get info on how to access these arguments in the Java code?

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>
Akbar
  • 1,513
  • 4
  • 20
  • 34
  • 1
    In Java code you have to analyze the args parameter of your `public static void main()` method. – khmarbaise Jan 30 '14 at 07:46
  • what do you mean "analyze"? I am looking for the code syntax to read them in the program? Also code on what should be mentioned in the command line? – Akbar Jan 30 '14 at 07:47
  • @BrianRoach - This does not answer my second part. How to access these args in the Java program? – Akbar Jan 30 '14 at 07:59
  • Um. http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – Brian Roach Jan 30 '14 at 08:07
  • @BrianRoach - This does not work for me as i am using TestNG and not calling the main method. You should read the question properly before answering or marking duplicates. – Akbar Jan 30 '14 at 08:13
  • @TestAutomationEngr Call me crazy, saw an example using an exec plugin and listing a main class, asking about command line arguments. I'm not quite sure what form of ESP would be required to come up with TestNG and test methods from that, but I don't have it. You would need to set named properties and explicitly read them as Aaron answers below. – Brian Roach Jan 30 '14 at 08:21
  • This SO answer should help. I do this with the Riak Java client also using the surefire plugin as in the answer -> http://stackoverflow.com/questions/3231797/specify-system-property-to-maven-project – Brian Roach Jan 30 '14 at 08:26

1 Answers1

0

There is no easy way to access command line arguments. The usual workaround is to use System properties instead:

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

You can access these anywhere using System.getProperties()

Related articles:

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820