2012-04-24 53 views

回答

0

例如:build.properties包含

username = root 
password = shoot 

爲屬性,那麼你可以改變的persistence.xml有像

<username>@[email protected]</username> 
<password value="@[email protected]"/> 

值,那麼你的build.xml應該是這樣的意志工作。

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

這裏$ username和$ password值自動螞蟻從<property>標籤標識,你可以用鑰匙作爲名稱訪問值。

+0

我怎麼可以將build.properties的值注入到persistence.xml?這些鏈接沒有顯示persistence.xml。當build.xml正在運行時,我需要從build.properties獲取值到persistence.xml ....這意味着值應該自動在persistence.xml中反射... – user1340856 2012-04-24 07:29:06

+0

請通過替換值與Ant任務鏈接這樣做。 – Phani 2012-04-24 07:32:34

+0

對不起。我沒有從這個鏈接得到任何想法。這個標籤實際上做了什麼? – user1340856 2012-04-24 07:39:01

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文件閱讀。玩的開心。

院長