【问题标题】:Unable to get <systemPropertyVariables> variable values from pom无法从 pom 获取 <systemPropertyVariables> 变量值
【发布时间】:2014-12-16 05:15:30
【问题描述】:

您好,我正在开发一个 java maven 项目,我必须在 pom.xml 文件中定义一些变量。

我在 pom.xml 文件中定义了如下变量。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.16</version>
        <configuration>
            <includes>
                <include>**/*Test*.java</include>
                <include>**/*Tests*.java</include>
                <include>**/Test*.java</include>
            </includes>
            <systemPropertyVariables>
                <my.value>NOTNULL</my.value>
            </systemPropertyVariables>
        </configuration>
    </plugin>

为了尝试访问 my.value 变量,我使用了以下 Java 代码。

    String testdata = System.getProperty("my.value");
    System.out.println(testdata);

但即使我设置了变量的值,控制台输出也总是显示null

谁能指出这里有什么问题?

提前致谢。

编辑:我也尝试在maven-failsafe-plugin 下声明systemPropertyVariables,但没有任何变化。

注意:当我尝试如下转换testdata行代码时,

   String testdata = System.getProperty("my.value").toString();

我在上面一行得到一个 NullPointer 异常。

编辑:很抱歉之前将此作为答案发布..

我正在使用您提供的插件.../plugin 代码将它作为 JUnit 测试运行,但这是我的控制台输出..

21 Oct 2014 12:36:56,973 main                                     INFO  s.MyClass                  - Default Implicit timeout set in Driver to: 100  
21 Oct 2014 12:36:56,973 main                                     INFO  s.MyClass                  - Default URL for server is set to: http://localhost:8080
 ---- null

URL 是我试图从 pom.xml 文件中检索的内容,而我所写的条件是

如果变量中的值没有以 ${ 开头,则返回 localhost:8080 否则返回 url。

所以如果你能指出我这里有什么问题

【问题讨论】:

  • 我从未听说过从你的 pom.xml 中访问变量。 Maven 是一个构建系统,与运行代码无关。除非您在谈论从 Maven 触发的测试?
  • @ScaryWombat 我正在为移动自动化运行 JUnit 测试。

标签: java eclipse windows maven pom.xml


【解决方案1】:

WindowsJDK 1.6.0_67 上使用maven-3.2.3 为我工作

使用maven-archetype-quickstart创建了一个项目...

添加了相关的 pom 行...将surefire example 与上述问题中的特定行结合起来。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
      <systemPropertyVariables>
        <my.value>NOTNULL</my.value>
        <buildDirectory>${project.build.directory}</buildDirectory>
      </systemPropertyVariables>
    </configuration>
  </plugin>

AppTest.java 中的相关行

/**
 * Rigourous Test :-)
 */
public void testApp()
{
    System.out.println(System.getProperty("my.value"));
    System.out.println(System.getProperty("buildDirectory"));
    assertTrue( true );
}

来自mvn test的相关输出

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
NOTNULL
C:\Users\raghu\Documents\GitHub\mvn-examples\test-properties\target
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in c
om.mycompany.app.AppTest

Here是github中的项目。

【讨论】:

  • 我是否必须在任何地方指定 project.build.directory,如果是,它的值应该是什么?
  • 我正在使用 appium 为移动 webpae 自动化运行 JUnit 测试,因此当我将项目作为 Maven 测试运行时,当我实例化我的 AppiumDriver 时会引发错误。知道该怎么做吗?
  • buildDirectory 只是一个示例值。你可以看上面的pomsn-p 看看它的值是如何设置的。
【解决方案2】:

@Raghuram 感谢您的帮助。

我有办法解决这个问题。

在我的 java 文件中,在 @Before 注释中,我将变量的值设置如下

 System.setProperty("my.value","this value");

现在它工作得很好。

干杯。

注意:各位,我很抱歉发布另一个问题作为答案.. :(

【讨论】:

    【解决方案3】:

    您在这里偶然发现了 Eclipse IDE 的 known limitation(问题标记为 eclipse)。

    虽然 Eclipse 之外的 mvn test 命令的工作方式类似于 Eclipse IDE 中的“Maven 测试”运行配置,但“JUnit 测试”运行配置是运行测试代码的 Eclipse IDE 的完全不同的部分。并且 JUnit Eclipse 集成(至少到我正在运行的 JUnit 版本 4)没有与 M2Eclipse 插件(将 Maven 功能带入 Eclipse 的插件)集成。因此 Eclipse 中的 JUnit 特性不知道 Maven 特性,反之亦然。因此:

    • 您在 JUnit 测试结果视图中看不到 Maven test 结果
    • JUnit 不会读取您的 Maven 配置文件或任何其他 Maven 特定配置(如 systemPropertyVariablessurefire 配置)

    在@Raghuram 的回答中,他之所以成功,是因为他正在运行mvn test(Eclipse 外部)或“Maven 测试”(Eclipse 内部)。如果他还使用 JUnit Eclipse 集成运行测试代码,他还将获得这些属性的 null 值。

    你有什么解决方案:

    • 正如@Raghuram 的回答所指出的,使用 Maven 运行测试有以下缺点:

      • 您无法从 ui 轻松运行单个测试或一组测试;您必须编辑该 Maven 运行配置并在那里提供测试过滤参数。我猜你会比在 Eclipse 之外使用终端更快
      • 您不会在 JUnit 视图中看到“Maven 测试”结果(它本身有一些方便的功能)
    • 使用其他 IDE(如 InteliJ IDEA)有以下缺点:

      • 它不是免费的
      • 学习新的 IDE
    • 使用Eclipse外部的终端运行测试有以下缺点:

      • 您会想念加速故障排除的 IDE 功能
      • 您无法像以前在 IDE 中那样进行调试

    【讨论】:

    • 这就是我的怀疑... ? 但是,IntelliJ 社区版是开源免费的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多