Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

pass a java parameter from maven

I need to execute some tests with maven, and pass a parameter from the command line.

My java code should get the parameter as: System.getenv("my_parameter1");

and I define the parameter in the pom.xml file as the example below: (and latter, I'd modify the pom.xml to get the parameter from the common line mvn clean install -Dmy_parameter1=value1)

but it does not work; System.getenv("my_parameter1") returns null. how should I define the parameter in the pom.xml file?

pom.xml

<project>
  ...
  <profiles>
    <profile>
      <properties>
        <my_parameter1>value1</my_parameter1>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
              <execution>
                <id>slowTest</id>
                <phase>test</phase>
                <goals>
                  <goal>test</goal>
                </goals>
                <configuration>
                  <skip>false</skip>
                  <includes>
                    <include>**/*Test.java</include>
                    <include>**/*TestSlow.java</include>
                  </includes>
                  <properties>
                    <my_parameter1>value1</my_parameter1>
                  </properties>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

System.getenv() reads environment variables, such as PATH. What you want is to read a system property instead. The -D[system property name]=[value] is for system properties, not environment variables.

You have two options:

  1. If you want to use environment variables, use the OS-specific method of setting the environment variable my_parameter1 before you launch Maven. In Windows, use set my_parameter1=<value>, in 'nix use export my_parameter1=<value>.

  2. You can use System.getProperty() to read the system property value from within your code.

example:

String param = System.getProperty("my_parameter1");

In you surefire plugin configuration, you can use:

<configuration>
    <systemPropertyVariables>
        <my_property1>${my_property1}</my_property1>
    </systemPropertyVariables>
</configuration>

Which takes the Maven property _my_property1_ and sets it also in your tests.

More details about this here.

I'm not sure if system properties from Maven are automatically passed to tests and/or whether fork mode affects whether this happens, so it's probably a good idea to pass them in explicitly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...