2

在我们的项目中,我需要从 build.properties 获取一些值到 persistence.xml。请让我知道是否有任何可能的方法?

4

2 回答 2

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 回答
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 值由 ant 从<property>标签中自动识别,您可以使用键作为名称访问值。

于 2012-04-24T07:16:57.193 回答