微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从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");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。