2

参数化 Maven 脚本以在 Spring 配置之间切换的最佳方法是什么?

我让 Maven 为 Web 应用程序构建了一个 WAR 文件。我有替代弹簧配置 - 一种用于与模拟对象的集成测试,一种用于与真实对象的实时生产使用。

理想情况下,我希望有一个可以构建任一 WAR 文件的 Maven 构建脚本。目前,我只是在构建之前破解 spring 配置文件,评论进出模拟和真实对象。

解决此问题的最佳方法是什么?

4

1 回答 1

5

我建议您使用构建配置文件

对于每个配置文件,您将定义一个特定的 Spring 配置:

<profiles>
        <profile>
            <id>integration</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env</name>
                    <value>integration</value>
                </property>
            </activation>
            <!-- Specific information for this profile goes here... -->
        </profile>

        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env</name>
                    <value>production</value>
                </property>
            </activation>
            <!-- Specific information for this profile goes here... -->
        </profile>
...

然后,您将通过设置参数env来激活一个或另一个配置文件:-Denv=integration对于第一个配置文件,-Denv=production对于第二个配置文件。

在每个profile块中,您可以指定特定于您的环境的任何信息。然后,您可以指定propertiesplugins等。在您的情况下,您可以更改资源插件的配置以包含足够的 Spring 配置。例如,在集成配置文件中,您可以指定 Maven 将在哪里搜索 Spring 配置文件:

<profile>
    <id>integration</id>
    <activation>
        <activeByDefault>false</activeByDefault>
        <property>
            <name>env</name>
            <value>integration</value>
        </property>
    </activation>
    <build>
        <resources>
            <resource>/path/to/integration/spring/spring.xml</resource>
        </resources>
    </build>
</profile>
于 2010-01-27T14:02:46.660 回答