如何从 build.properties 获取值到持久性.xml



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

例如:build.properties 包含

 username = root
 password = shoot

作为属性,您可以更改持久性.xml具有类似

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

那么你的构建.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> 标签中自动识别,您可以访问带有键作为名称的值。

我不得不自己回忆一下。 正确的做法是这样的

    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 文件中读取它。 玩得愉快。

院长

最新更新