【问题标题】:How can I configure environment variables in maven surefire per environment?如何在每个环境中配置 Maven Surefire 中的环境变量?
【发布时间】:2018-05-25 15:00:35
【问题描述】:

我正在尝试在竹子构建计划期间运行 Maven 测试阶段。我的测试依赖于我在 pom.xml 中配置的一组环境变量。但是,这些值仅适用于一个环境,我想在多个环境(不同的主机 IPS 等)上执行相同的测试。我的 Pom.xml 看起来像这样:

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>

<configuration>

 <environmentVariables>
    <variableName>variableValue</variableName>
  </environmentVariables>

</configuration>

竹计划构建有一个阶段,它使用简单的“mvn clean test”命令行执行 maven 测试阶段。我有超过 10 个环境变量,所以我希望避免将这些变量值传递给命令行,因为它会使它变得很长。如何配置 maven Surefire 以包含一组以上的环境变量(一组用于测试,一组用于生产),这样我就可以根据我执行的竹子构建计划传递到命令行以获取环境变量。所以类似:mvn clean -D test。

【问题讨论】:

    标签: maven bamboo maven-surefire-plugin


    【解决方案1】:

    吹捧问题的最佳解决方案是使用配置文件过滤和属性文件。 您需要配置以下文件夹树:

      • 测试
        • java
        • 配置文件资源
        • 资源

    您需要将一个属性文件放入资源中,并将变量值声明为配置文件资源的链接: 变量={值} 并且您需要在 profile-resources 中为每个环境创建单独的属性文件。

    然后将其包含在您的 pom.xml 文件中:

      <profiles>
        <profile>
            <id>Name of first profile</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <filter.properties>env1.properties</filter.properties>
            </properties>
        </profile>
        <profile>
            <id>Name of second profile</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <filter.properties>env2.properties</filter.properties>
            </properties>
        </profile>
    </profiles>
    
    <build>
        <filters>
            <filter>src/test/profile-resources/${filter.properties}</filter>
        </filters>
    </build>
    

    然后您可以使用激活配置文件的 -P Maven 命令自动调用每个配置文件。 示例: mvn verify -P "配置文件名称"。

    相关资源:

    【讨论】:

    • 好的,首先非常感谢。我实际上采用了这种方法,直到我意识到我不知道如何在 Java 中创建一个可以从正确的文件中获取环境变量的方法。我正在使用 Java 从类路径上的文件中获取属性,那么如何动态加载正确的类路径文件?我想要做的例子可以在这里找到:mkyong.com/java/java-properties-file-examples
    • 使用配置文件过滤器,您的文件始终称为 env.properties,但实际的 env 属性文件称为 dev_env.properties、production_env.properties。 Maven 将确保当您选择 dev 配置文件时,env.properties 文件会从 dev_env.properties 加载其属性。这是属性代码: properties = new Properties();输入流 inputStream = null;尝试 { inputStream = PropertyUtil.class.getResourceAsStream("/env.properties"); properties.load(inputStream)
    • 这是文件结构:link 这是 env.properties 的样子:property1=${property1} property2=${property2} property3=${property3} 这是 dev_env.properties 的样子: property1=api.url property2=website.url property3=api.port Maven 会根据配置文件自动加载属性文件。您总是在代码中加载 env.properties 文件。
    • 谢谢,那么我的 env.properties 文件会是什么样子?这有意义吗:dev_env.properties -> hostname = devhost, prod_env.properties -> hostname = prodhost, env.properties -> hostname = ${hostname}?
    • 是的,就是这样,你明白了:D 在 env.properties 文件中,你只需使用对其他文件中其他属性的引用,恭喜。
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2014-08-05
    相关资源
    最近更新 更多