Java无法从pom中读取变量

时间:2014-10-20 11:16:49

标签: java eclipse maven

我的maven.properties文件中有以下行。

   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>

最后我有一段代码来检索消息变量中的值。

   public static String getMsg() 
   {
      final String msg = System.getProperty("message");
      if (StringUtils.isEmpty(msg) || url.startsWith("${")) 
      {
          return "Empty message";
      }
      return msg;
   }

因此,当我调用getMsg()方法时,它总是将msg的值作为空消息返回。

是pom.xml声明中的某个错误还是正在使用的getMsg()函数中的某个问题。

如果有人可以对此有所了解,那就太好了。

提前致谢....

3 个答案:

答案 0 :(得分:1)

如果我没有弄错,您需要使用以下语法

<properties>
  <message>${MY_VARIABLE}</message>
</properties>

systemPropertyVariables用于Surefire插入,用于单元测试

答案 1 :(得分:0)

我前段时间遇到过这个问题。我不知道它是否是故障保护/ maven错误或限制。我发现的解决方案是将变量设置为VM参数。以下是它目前对我有用的方式:

按如下方式更新你的邮件:

                   <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来运行集成测试,如下所示:     mvn integration-test -DMY_VARIABLE = www.google.com

答案 2 :(得分:0)

感谢各位帮忙..

我找到了完成这项工作的方法。

在我开始测试之前,在我的java文件中我正在使用

 System.setProperty("message" , "welcome message");

这最终会打印出值而不是null。

干杯

相关问题