2

私たちのプロジェクトでは、build.properties から persistence.xml にいくつかの値をフェッチする必要があります。可能な方法があれば教えてください。

4

2 に答える 2

0

例:build.propertiesには

 username = root
 password = shoot

プロパティとして、persistence.xmlを次のような値に変更できます

 <username>@username@</username>
 <password value="@password@"/> 

その場合、build.xmlは次のように機能するはずです。

<target name="replace">
    <property file="build.properties"/>
    <replace file="persistence.xml" token="@username@" value="${username}"/>
    <replace file="persistence.xml" token="@password@" value="${password}"/>
</target>

ここで、$usernameと$passwordの値は、<property>タグからantによって自動的に識別され、名前としてkeyを使用して値にアクセスできます。

于 2012-04-24T07:16:57.193 に答える
0

私は自分でそれを思い出さなければなりませんでした。それを行う正しい方法はそのようなものです

    Map<String, String> props = new HashMap<String, String>();
    props.put("hibernate.connection.username", "sa");
    props.put("hibernate.connection.password", "");
    props.put("hibernate.connection.url", "jdbc:h2:mem:test_mem;MVCC=true");
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("inmemory", props); 

一時的に行ったようにユーザー名をハードコーディングする代わりに、build.properties ファイルから読み取ることができます。楽しむ。

ディーン

于 2012-09-07T14:53:47.210 に答える