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

时间:2010-01-27 13:53:49

标签: spring maven

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

我让Maven为网络应用构建一个WAR文件。我有另外的弹簧配置 - 一个用于与模拟对象进行集成测试,一个用于与真实对象一起实时生产。

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

最好的方法是什么?

1 个答案:

答案 0 :(得分:5)

我建议你使用build profiles

对于每个配置文件,您将定义一个特定的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>