简体   繁体   English

从pom.xml java获取变量

[英]Getting variables from pom.xml java

I am using eclipse with maven for a mobile automation test on a mobile webpage. 我正在使用eclipse和maven在移动网页上进行移动自动化测试。

I am defining the following in my pom.xml 我在我的pom.xml中定义了以下内容

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

but when i am calling this using 但是当我打电话给这个时

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

it always seems to return null. 它总是似乎返回null。

I also tried the following way of defining variable 我也尝试了以下定义变量的方法

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

but still am getting the value as null. 但仍然得到值为null。

I could use some help Thanks. 我可以用一些帮助谢谢。

Your configuration will not work in eclipse since there is no good m2e support for surefire. 你的配置在eclipse中不起作用,因为没有好的m2e支持surefire。 The maven surefire plugin forkes a new process and provides the systemPropertyVariables to it. Maven的万无一失插件forkes一个新的进程,并提供systemPropertyVariables它。 Your configuration will work if you run the tests from the command-line, eg 如果从命令行运行测试,则配置将起作用,例如

mvn surefire:test

To make it run in both worlds (command-line and eclipse) I do it this way... 为了让它在两个世界中运行(命令行和日食)我这样做...

  1. Create a src/test/resources/maven.properties 创建一个src/test/resources/maven.properties
  2. Edit the maven.properties file and put the properties you need in it, eg 编辑maven.properties文件并将所需的属性放入其中,例如

     project.build.directory=${project.build.directory} MY_VARIABLE=${MY_VARIABLE} 
  3. Enable resource filtering for test resources 为测试资源启用资源过滤

     <build> <testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> ... </build> 
  4. Load the properties in your test and access them 在测试中加载属性并访问它们

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM