2012-04-24 32 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文件阅读。玩的开心。

院长