Java无法从pom读取变量 [英] Java unable to read variables from pom

查看:413
本文介绍了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-failuresafe-plugin< / artifactId>
< version> 2.16< / version>

<执行>
< execution>
< phase> validate< / phase>
< goals>
< goal> read-project-properties< / goal>
< / goals>
< configuration>
< includes>
< include> ** / * Test * .java< / include>
< include> ** / *测试* .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 msg;
}

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



在pom.xml声明中是否有一些错误,或者是正在使用的getMsg()函数中有些问题。



有人可能会对此有所裨益。



提前感谢....

解决方案

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

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

systemPropertyVariables用于Surefire pluging,用于单元测试


I have the following line in my maven.properties file.

   MY_VARIABLE = www.google.com

My pom file code looks like this

    <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>

And finally i have a piece of code to retrieve the value in the message variable.

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

So when i am calling the getMsg() method, it always returns the value of msg as Empty Message.

Is it some error in the pom.xml declaration or is it some problem in the getMsg() function that is being used.

It would be nice of someone could throw some light on this.

Thanks in advance ....

解决方案

If I'm not mistaken, you need to use the following syntax

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

systemPropertyVariables are for the Surefire pluging, for unit testing

这篇关于Java无法从pom读取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆