tags:

views:

43

answers:

1

What is the best way to parameterise a Maven script to switch between Spring configurations?

I have Maven building a WAR file for a web app. I have alternative spring configurations - one for integration testing with mock objects, one for live production use with real objects.

Ideally, I would like to have one Maven build script that can build either WAR file. At present, I just hack the spring config file before building, commenting in and out the mocks and real objects.

What is the best way to go about this?

+4  A: 

I suggest that you use the build profiles.

For each profile, you will define a specific Spring configuration:

<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>
...

You will then activate one profile or the other by setting the parameter env : -Denv=integration for the first profile, -Denv=production for the second profile.

In each profile block, you can specify any information specific to your environment. You can then specify properties, plugins, and so on. In your case, you may change the configuration of the resources plugin in order to include the adequate Spring configuration. For example, in integration profile, you can specify where Maven will search the Spring configuration file:

<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>
romaintaz