2

Spring構成を切り替えるためにMavenスクリプトをパラメータ化する最良の方法は何ですか?

Maven で Web アプリ用の WAR ファイルを作成しています。別のスプリング構成があります.1 つはモック オブジェクトとの統合テスト用、もう 1 つは実際のオブジェクトを使用したライブ プロダクション用です。

理想的には、いずれかの WAR ファイルをビルドできる 1 つの Maven ビルド スクリプトが必要です。現在、ビルドする前にスプリング構成ファイルをハックし、モックと実際のオブジェクトをコメントインおよびコメントアウトしています。

これについて最善の方法は何ですか?

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は 2 番目のプロファイルです。

profileブロックでは、環境に固有の情報を指定できます。propertiesその後、、、などを指定できますplugins。あなたの場合、適切な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 に答える