从pom.xml java获取变量

我正在将Eclipse与Maven一起用于移动网页上的移动自动化测试.

我在pom.xml中定义以下内容

<properties>
    <MY_VARIABLE>www.google.com/</MY_VARIABLE>
</properties>

但是当我使用

String testurl1 = System.getProperty("MY_VARIABLE");

它似乎总是返回null.

我也尝试了以下定义变量的方法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <systemPropertyVariables>
        <MY_VARIABLE>www.google.com</MY_VARIABLE>
        </systemPropertyVariables>
    </configuration>
</plugin>

但仍然将值设为null.

我可以帮忙
谢谢.

解决方法:

您的配置无法在Eclipse中使用,因为对surefire的m2e没有很好的支持. Maven surefire插件创建了一个新进程并为其提供了systemPropertyVariables.如果您从命令行运行测试,则配置将起作用,例如

mvn surefire:test

为了使它在两个环境(命令行和日食)中都可以运行,我这样做是…

>创建一个src / test / resources / maven.properties
>编辑maven.properties文件,然后将所需的属性放入其中,例如

project.build.directory=${project.build.directory}
MY_VARIABLE=${MY_VARIABLE}

>为测试资源启用资源过滤

<build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    ...
</build>

>在测试中加载属性并访问它们

 Properties mavenProps = new Properties();
 InputStream in = TestClass.class.getResourceAsstream("/maven.properties");
 mavenProps.load(in);

 String buildDir = mavenProps.getProperty("project.build.directory");
 String myVar = mavenProps.getProperty("MY_VARIABLE");

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念