Java无法从pom读取变量
我的maven.properties文件中有以下行Java无法从pom读取变量,java,eclipse,maven,Java,Eclipse,Maven,我的maven.properties文件中有以下行 MY_VARIABLE = www.google.com 我的pom文件代码如下所示 <build> <pluginManagement> <testResources> <testResource> <directory>src/test/resources</directory&
MY_VARIABLE = www.google.com
我的pom文件代码如下所示
<build>
<pluginManagement>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<includes>
<include>**/*Test*.java</include>
<include>**/*Tests*.java</include>
<include>**/Test*.java</include>
</includes>
<files>
<file>src/test/resources/maven.properties</file>
</files>
<systemPropertyVariables>
<message>${MY_VARIABLE}</message>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
</plugins>
</pluginManagement>
</build>
因此,当我调用getMsg()方法时,它总是以空消息的形式返回msg的值
是pom.xml声明中的错误还是正在使用的getMsg()函数中的问题
要是有人能解释一下就好了
提前感谢….如果我没弄错,您需要使用以下语法
<properties>
<message>${MY_VARIABLE}</message>
</properties>
${MY_VARIABLE}
systemPropertyVariables用于可靠的Fire插件,用于单元测试我不久前遇到过这个问题。我不知道这是故障保护/maven错误还是限制。我找到的解决方案是将变量设置为VM参数。以下是我目前的工作方式: 按如下方式更新您的pom:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>-DMY_VARIABLE=${MY_VARIABLE}</argLine>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
maven故障保护插件
2.17
-DMY_VARIABLE=${MY_VARIABLE}
测试
集成测试
验证
然后,您可以调用maven来运行集成测试,如下所示:
mvn集成测试-DMY_VARIABLE=www.google.com感谢大家的帮助 我找到了一个方法来完成这件事 在开始测试之前,在我的java文件中,我正在使用
System.setProperty("message" , "welcome message");
这就是最终打印出的值,而不是空值
干杯我在这里有一个疑问,因为MY_变量是在另一个目录(src/test/resources)中的maven.properties文件中声明的,如果我直接执行此操作,它不会引发错误。。如何首先导航到该目录?问题可能是您在单元测试中使用了错误的插件。从maven网站“Failsafe插件设计用于运行集成测试,而Surefire插件设计用于运行单元测试”。如果您打算运行单元测试,您可能希望更改为Surefire插件。感谢您的回复,但它也不能与Surefire插件一起工作..:(我不熟悉从命令行运行maven,我将它用于我的JUnit mobile automation脚本,因此您可以进一步解释一下。只需再回答一个问题。由于my_变量位于另一个文件中,我如何访问它。您可以在命令行中直接提供变量的值,或者使用中间shell变量。)例如,对于unix系统,它看起来像:MY_VARIABLE=$(cat maven.properties)我正在windows上使用eclipse,所以您可以举一个例子来描述您所说的。
System.setProperty("message" , "welcome message");