たなちの備忘録

自分の知識をストックしていくためのブログ

【Maven】環境毎にプロパティファイルを入れ替える方法

スポンサーリンク

DBの接続情報などを記載したプロパティファイルを環境毎に設定したい場合の方法。

pom.xmlにmaven-war-pluginのプラグインを設定する。

Mavenプロジェクトはデフォルトだとsrc/main/resourcesの内容がtarget/classesとWARファイル内のWEB-INF/classesになるが、このプラグインで指定した内容でファイルを置き換えることができる。

maven-war-plugin

2018年6月時点で最新バージョンは3.2.2。

profile

profileに環境毎の設定ファイルを定義する。local, dev, stg, prd環境毎に設定ファイルを用意した。

今回は「contextxml.path」、「resources.path」が対象のファイルパス。pluginで設定する変数(${contextxml.path}、${resources.path})で参照される。

activeByDefaultをtrueにすると、何も指定しない場合その設定がデプロイされる。

 <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <contextxml.path>src/local/webapp/META-INF</contextxml.path>
                <resources.path>src/local/resources</resources.path>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <contextxml.path>src/dev/webapp/META-INF</contextxml.path>
                <resources.path>src/dev/resources</resources.path>
            </properties>
        </profile>
        <profile>
            <id>stg</id>
            <properties>
                <contextxml.path>src/stg/webapp/META-INF</contextxml.path>
                <resources.path>src/stg/resources</resources.path>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <contextxml.path>src/prd/webapp/META-INF</contextxml.path>
                <resources.path>src/prd/resources</resources.path>
            </properties>
        </profile>
    </profiles>

plugin

resourceタグに設定を行う。directoryタグに参照するリソースパスを指定し、targetPathに上書きされるディレクトリを指定する。

     <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${resources.path}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                            <includes>
                                <include>test.properties</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>${contextxml.path}</directory>
                            <targetPath>META-INF</targetPath>
                            <filtering>true</filtering>
                            <includes>
                                <include>context.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

パッケージを生成してみる

-P オプションで環境を指定すると、その設定ファイルがtargetPathタグで指定したディレクトリにコピーされる。targetディレクトリ以下や生成したwarファイルの中身を確認して置き換わっていれば成功。

mvn -P stg package -DskipTests=true

以下、参考記事

maven-war-pluginで環境ごとの設定ファイルを管理してみた - Challenge Java EE !

「Apache Maven WAR Plugin」で環境ごとの設定ファイルを変更する方法とシクハクしたところまとめてみました[文字化け] | レッツトライ!しもしも