我需要用maven执行一些测试,并从命令行传递一个参数.
我的java代码应该获取参数:System.getenv("my_parameter1");
我在pom.xml文件中定义参数,如下例所示:(后者,我修改pom.xml以从公共行获取参数mvn clean install -Dmy_parameter1 = value1)
但它不起作用; System.getenv("my_parameter1")返回null.我应该如何在pom.xml文件中定义参数?
的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>
Run Code Online (Sandbox Code Playgroud)
pru*_*nge 17
System.getenv()读取环境变量,例如PATH
.你想要的是改为读取系统属性.-D [系统属性名称] = [value]用于系统属性,而不是环境变量.
您有两种选择:
如果要使用环境变量,请my_parameter1
在启动Maven之前使用特定于操作系统的方法设置环境变量.在Windows中,使用set my_parameter1=<value>
'nix使用export my_parameter1=<value>
.
您可以使用System.getProperty()从代码中读取系统属性值.
例:
String param = System.getProperty("my_parameter1");
Run Code Online (Sandbox Code Playgroud)
在您的surefire插件配置中,您可以使用:
<configuration>
<systemPropertyVariables>
<my_property1>${my_property1}</my_property1>
</systemPropertyVariables>
</configuration>
Run Code Online (Sandbox Code Playgroud)
它接受Maven属性_my_property1_并在测试中设置它.
关于这里的更多细节.
我不确定Maven的系统属性是否会自动传递给测试和/或fork模式是否影响是否会发生这种情况,因此明确传递它们可能是个好主意.
归档时间: |
|
查看次数: |
23224 次 |
最近记录: |