【问题标题】:Best practice for defining machine specific resources in maven builds?在 Maven 构建中定义机器特定资源的最佳实践?
【发布时间】:2013-02-27 19:05:41
【问题描述】:

是否有一种标准方法可以在 Maven 构建中配置特定于环境的资源?

例如 - 我们希望我们的构建将在应用程序中使用的本地服务的特定 IP 地址不同的环境中运行。

一种选择是将其设置为 shell 环境变量,但不清楚这是否会传播到运行单元测试的surefire jvm。

另一种选择是在 pom.xml 子类文件中提供此信息,但这会带来其他包袱(每个开发人员都需要维护自己的 pom 文件),这当然会破坏任何类型的自动构建环境。

【问题讨论】:

    标签: shell maven environment-variables


    【解决方案1】:

    以下示例显示了如何使用构建配置文件来获取不同的属性值集。

    示例

    您可以使用 -P 参数来激活其中一个构建配置文件

    $ mvn -Ptest1 compile
    ..
    [INFO] --- maven-antrun-plugin:1.7:run (default) @ demo ---
    [INFO] Executing tasks
    
    main:
         [echo] arbitrary.property=1.0
    ..
    

    切换配置文件获取与第二个配置文件关联的属性值:

    $ mvn -Ptest2 compile
    ..
    [INFO] --- maven-antrun-plugin:1.7:run (default) @ demo ---
    [INFO] Executing tasks
    
    main:
         [echo] arbitrary.property=2.0
    ..
    

    pom.xml

    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <configuration>
                                <target>
                                    <echo message="arbitrary.property=${arbitrary.property}"/>
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <profiles>
            <profile>
                <id>test1</id>
                <properties>
                    <arbitrary.property>1.0</arbitrary.property>
                </properties>
            </profile>
            <profile>
                <id>test2</id>
                <properties>
                    <arbitrary.property>2.0</arbitrary.property>
                </properties>
            </profile>
        </profiles>
    </project>
    

    【讨论】:

      【解决方案2】:

      Surefire 尽最大努力确保分叉的 JVM 与用户环境的变化尽可能隔离。如果你想通过thin,你需要使用systemPropertyVariables 配置选项来定义分叉JVM的系统属性。

      其他人提到了个人资料。一般来说,使用配置文件来注入特定于环境的细节是一个糟糕的计划,甚至是 Maven 反模式。只有一种情况是这样的配置文件不是反模式(注意我不是在推广一种模式,只是移出反模式类别),这是您的配置文件调整测试的地方> 环境您没有将tests.jar 附加到反应堆。在这种情况下,“调整过的”工件不会逃脱其模块以导致“坏事”(例如,当存储库中的工件部署到存储库时,不确定哪个配置文件处于活动状态,或者构建是否使用来自本地仓库或具有不同配置文件的远程仓库)

      我会在 CLI 上使用系统属性,并使用 systemPropertyVariables 配置选项将其传递给集成测试。

      如果你想要更“maven 方式”的东西,你可能需要一个 maven 插件来启动要测试的服务,但这对于非基于 java 的服务来说可能非常困难。请参阅cassandra-maven-plugin 了解如何使用基于 Java 的服务来执行此类操作的示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多