您好,我正在开发一个 java maven 项目,我必须在其中的 pom.xml 文件中定义一些变量。
我在我的 pom.xml 文件中定义了一个变量,如下所示。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
<include>**/*Tests*.java</include>
<include>**/Test*.java</include>
</includes>
<systemPropertyVariables>
<my.value>NOTNULL</my.value>
</systemPropertyVariables>
</configuration>
</plugin>
为了尝试访问 my.value
变量,我使用了以下 Java 代码。
String testdata = System.getProperty("my.value");
System.out.println(testdata);
但控制台输出总是显示 null
,即使我设置了变量的值也是如此。
谁能指出这里有什么问题吗?
提前致谢。
编辑:我也试过在 maven-failsafe-plugin
下声明 systemPropertyVariables
但没有改变。
注意:当我尝试按如下方式转换 testdata 代码行时,
String testdata = System.getProperty("my.value").toString();
我在上面一行得到了 NullPointer 异常。
编辑:很抱歉之前将此作为答案发布..
我正在使用插件将其作为 JUnit 测试运行.../您提供的插件代码,但这是我的控制台输出..
21 Oct 2014 12:36:56,973 main INFO s.MyClass - Default Implicit timeout set in Driver to: 100
21 Oct 2014 12:36:56,973 main INFO s.MyClass - Default URL for server is set to: http://localhost:8080
---- null
URL 是我试图从 pom.xml 文件中检索的内容,我写的条件是
如果变量中的值是空的或以 ${ 开头,则返回 localhost:8080 否则返回 url。
所以如果你能指出我这里的错误
最佳答案
适用于 Windows
上的 maven-3.2.3
和 JDK 1.6.0_67
使用 maven-archetype-quickstart
创建了一个项目...
添加了相关的 pom 行...结合 surefire example在上面的问题中有特定的行。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<my.value>NOTNULL</my.value>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</plugin>
AppTest.java中的相关行
/**
* Rigourous Test :-)
*/
public void testApp()
{
System.out.println(System.getProperty("my.value"));
System.out.println(System.getProperty("buildDirectory"));
assertTrue( true );
}
mvn test
的相关输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
NOTNULL
C:\Users\raghu\Documents\GitHub\mvn-examples\test-properties\target
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in c
om.mycompany.app.AppTest
Here是github中的项目。
关于java - 无法从 pom 获取 <systemPropertyVariables> 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478926/