0

プロパティ ファイルは次のとおりです。

test.url=https:\\url:port
test.path=/home/folder
test.location=Location
test.version=1

そして、次の ant タスク:

タスクの 1 回の実行に対して一時的な値を渡すことができます。

ant -Dtest.path=new_path test_props

-D キーを使用して渡した値で test.path 値を上書きするにはどうすればよいですか? 順番に、同じ起動後、test.path の値は上記の値に変更されますか?

次のバリアントは機能しません。

<entry key="test.path" value="${test.path}"/>

また

<propertycopy name="test.path" from="${test_path}"/>
4

1 に答える 1

1

ファイルを永続的に変更したい場合は、taskを使用できます。

私は次のことをします:

default.properties.sample のようなサンプル プロパティ ファイルを作成します。

指定された -D プロパティを受け取るターゲットを作成し、通知された場合は、ファイル default.properties.sample を置き換えて、それを default.properties ファイルに保存します。default.properties.sample には次の行があります。

test.url=https:\\url:port
test.path=@test_path@
test.location=Location
test.version=1

アクションは、@test_path@ トークンを、-D パラメーターで通知されたプロパティの実際の値に置き換え、結果のファイルを default.properties として保存します。何かのようなもの:

<copy file="default.properties.sample" toFile="default.properties" />
<replace file="default.properties" token="@test_path@" value="${test.path}" />

-D パラメータが通知された場合にのみプロパティを置換するか、ファイルが毎回置換されるなど、いくつかの調整を行う必要があります。

パスなども必要に応じて調整する必要があります。


次のシナリオをテストしましたが、うまくいきました。

build.xml と default.properties.sample の 2 つのファイルを作成しました。その内容は次のとおりです。

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="BuildTest" default="showProperties" basedir=".">
    <property file="default.properties"/>

    <target name="showProperties">
        <echo message="test.property=${test.property}"/>
    </target>

    <target name="replace">
        <fail unless="new.test.property" message="Property new.test.property should be informed via -D parameter"/>
        <copy file="default.properties.sample" toFile="default.properties"/>
        <replace file="default.properties" token="@test_property@" value="${new.test.property}"/>
    </target>
</project>

default.properties.sample:

test.property=@test_property@

そして、次のテストを実行します。

デフォルトの実行:

C:\Filipe\Projects\BuildTest>ant
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

showProperties:
     [echo] test.property=${test.property}

BUILD SUCCESSFUL
Total time: 0 seconds

エラー制御:

C:\Filipe\Projects\BuildTest>ant replace
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

replace:

BUILD FAILED
C:\Filipe\Projects\BuildTest\build.xml:10: Property new.test.property should be      informed via -D parameter
Total time: 0 seconds

プロパティの置換:

C:\Filipe\Projects\BuildTest>ant replace -Dnew.test.property="This is a New Value"
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

replace:
     [copy] Copying 1 file to C:\Filipe\Projects\BuildTest

BUILD SUCCESSFUL
Total time: 0 seconds

置換後のプロパティ ファイル:

C:\Filipe\Projects\BuildTest>type default.properties
test.property=This is a New Value

その後の実行では、test.property の新しい値が存在します。

C:\Filipe\Projects\BuildTest>ant
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

showProperties:
     [echo] test.property=This is a New Value

BUILD SUCCESSFUL
Total time: 0 seconds

それがあなたが探しているものだと思います。

于 2012-08-16T13:54:39.263 に答える