2014-10-20 32 views
1

我在我的maven.properties文件中有以下行。Java无法读取pom变量

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()函数中存在一些问题。

这将是很好的人可以抛出一些光。

在此先感谢....

回答

1

如果我没有记错的话,你需要使用下面的语法

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

systemPropertyVariables都为神火pluging,单元测试

+0

我这里有一个疑问,因为MY_VARIABLE在maven.properties文件,该文件是在另一个目录中(src /测试/资源),如果我直接这样做将它不会引发错误声明.. 我如何导航到目录? – Ram 2014-10-20 11:33:03

+0

一个问题可能是您使用错误的插件进行单元测试。来自maven站点“Failsafe插件旨在运行集成测试,而Surefire插件旨在运行单元测试。”如果您打算运行单元测试,则可能需要更改为Surefire插件。 – 2014-10-20 11:47:05

+0

感谢您的回复,但它不能与surefire插件一起工作.. :( – Ram 2014-10-20 11:51:05

0

前段时间我遇到了这个问题。我不知道它是否是故障安全/ maven bug或限制。我找到的解决方案是将该变量设置为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> 

然后,你可以调用行家,让您的集成测试运行如下: MVN集成测试-DMY_VARIABLE = WWW .google.com

+0

我不熟悉从命令行运行maven我正在使用它为我的JUnit移动自动化脚本,所以你可以进一步解释一下。 – Ram 2014-10-20 11:35:15

+0

请参阅我的更新。 – 2014-10-20 12:01:12

+0

再次感谢您提出一个问题。 因为MY_VARIABLE在另一个文件中,我如何访问它。 – Ram 2014-10-20 12:02:38

0

谢谢你们的帮助..

我找到了一种方法来完成这件事。

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

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

,这是最后打印出的值,而不是空。

干杯