【Ant】プロパティ・ファイル(.properties)を扱うには

初めに

 <property name="key1" value="Hello world" />
でも値を設定できるが、build.xml 内にあるので、
可変になる部分は外出ししといた方が便利かと思う。
 で、プロパティ・ファイル(.properties)から値を取得できるので
簡単なサンプルを作成しとく。

[1] プロパティ・ファイル(.properties)から値を取得するには...

 * とりあえず、以下の二通りある。

  1-1) property を利用する場合
  1-2) loadproperties を利用する場合

例1-1 : property を利用する場合

 * build.properties から値をとってきて、表示するサンプル

build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="sample_target" name="SampleCodes">
    <property file="build.properties" />
    <target name="sample_target">
        <echo>${key1}</echo>
    </target>
</project>

build.properties

key1=Hello world from a file

出力結果

sample_target:
     [echo] Hello world from a file
BUILD SUCCESSFUL
Total time: 417 milliseconds

参考文献

http://qiita.com/siguremon/items/e26a13f73bc8e3b7a6db

例1-2 : loadproperties を利用する場合

 * build.properties から値をとってきて、表示するサンプル

build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="sample_target" name="SampleCodes">
 <target name="sample_target">
    <loadproperties srcFile="build.properties">
      <filterchain>
        <linecontains>
          <contains value="import."/>
        </linecontains>
      </filterchain>
    </loadproperties>
    <echo>${key2}</echo>
 </target>
</project>

build.properties

key1=Hello world from a file
key2=Hello world2 from a file

出力結果

sample_target:
     [echo] Hello world2 from a file
BUILD SUCCESSFUL
Total time: 313 milliseconds

参考文献

http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/CoreTasks/loadproperties.html

[2] プロパティ・ファイル(.properties)を作成するには...

 * propertyfile を利用する

例2 : propertyfile を利用する

 * build.properties をAntビルドを作成するサンプル

build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="create.conf" name="SampleJavaCodes">
    <target name="create.conf">
      <propertyfile file="build.properties" comment="Sample properties">
        <entry  key="key1" value="value1"/>
        <entry  key="key3" value="value3"/>
      </propertyfile>
    </target>
</project>

出力結果 : build.properties

#Sample properties
#Thu, 15 Jan 2015 22:48:56 +0900

key1=value1
key3=value3

参考文献

http://www.ne.jp/asahi/hishidama/home/tech/ant/tag/property.html

関連記事

Antビルドのサンプルを作成してみる

http://blogs.yahoo.co.jp/dk521123/33914928.html

build.xml の書き方 ~task編~

http://blogs.yahoo.co.jp/dk521123/33891786.html