13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mavenで環境依存の値をバージョン管理の外に出す

Last updated at Posted at 2015-04-21

ファイルのパスは、環境ごとに異なるものです。
バージョン管理内に入れていると、変更ありになってしまったりして面倒です。
というわけで、Mavenでそういった環境依存の値をバージョン管理の外に出す構成と記述の一例をまとめます。
動かすための一連の内容なので、個々の詳細な解説は他に譲ります。
あくまで「Mavenではこんな感じでできる」の例です。フレームワークなど、それぞれの方法があれば、そちらの作法に従うようにしましょう。

具体的には、resourcesの下にあるファイルの文字列をビルド時に置換します。

1. pom.xmlで、プロパティとフィルタリングの設定をする

pom.xmlの中に、以下の様な記述をします。
<directory>以下のファイル内で、${foo}という文字列を${bar}の内容に書き換えます。
${bar}の実際の値は、~/.m2/settings.xml で定義します。(後述)

pom.xml
<properties>
  <foo>${bar}</foo>
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

2. ~/.m2/settings.xmlを作成し、プロパティの値を定義する

maven実行ユーザーの ~/.m2/ に settings.xml というファイルを、以下のような内容で作成します。
※既に settings.xml がある場合は、<profile> タグを増やしましょう。

settings.xml
<settings xmlns="http://maven.apache .org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <profiles>
    <profile>
      <id>sample-profile</id>
        <activation>
          <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
          <bar>/home/marrontan619/sample.txt</bar>
        </properties>
    </profile>
  </profiles>
</settings>

この sample-profile のプロファイルをアクティブにすることで、pom.xml内の ${bar} が /home/marrontan619/sample.txt にとして認識されます。

3. 置換される文字を含んだプロパティファイルを、プロジェクトの /src/resources/ の下に作成する

Java から読み込むときに便利なので、拡張子は.properties にしておきましょう。

config.properties
baz=${foo}

4. ビルドする

2 で作成したプロファイルをアクティブにするには、いくつか方法があります。
以下のいずれかの方法1つで十分です。

  1. eclipse で開発している場合は、プロジェクトを選択→Alt+Enter→Mavenを選択→アクティブ Maven プロファイルに、(今回の例では)"sample-profile" と入力する。
  2. settings.xml 内の <activeByDefault> の値を true にする。
  3. mvn コマンドの -P オプションに、(今回の例では)"sample-profile" を渡す。
    例: mvn clean install -P sample-profile

ビルドして出来た config.properties の中身は、こんな感じになります。

config.properties(成果物)
baz=/home/marrontan619/sample.txt

5. Java から利用する

順番としては、これをやらないとビルドする意味もないのですが、あくまで今回ここはおまけです。

Sample.java
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");
String baz = bundle.getString("baz");

baz の中身は "/home/marrontan619/sample.txt" になります。

以上、何番煎じだという感じの内容でしたが、自分がハマったところをまとめてみました。

pom.xml 内で、アクティブになっているプロファイルからプロパティを読み込めず、置換が出来ない場合はビルドエラーにしたいのですが、いい感じの書き方ありませんか?
教えてエロい人。

その他、気づいたことやご指摘等ございましたら、バシバシください。

注記というか言い訳

foo と bar は同じ名前にするのが通常です。
また、そうすれば pom.xml に<properties> のタグは必要ありませんが、プロファイルが選択されなかった場合に、config.properties の内容が置換されない状態で成果物ができてしまうので、ビルドエラーを出すためにも記述してあります。
本記事では、どこの記述がどこに対応しているかを明確にさせるために、このようになっています。

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?