简体   繁体   中英

how to fetch a value from build.properties to persistence.xml?

In our project I need to fetch some values from build.properties to persistence.xml. Please let me know if there is any possible way?

For example: build.properties contains

 username = root
 password = shoot

as properties then you could change the persistence.xml to have values like

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

then your build.xml should be something like this will work.

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

Here $username and $password values are automatically identified by ant from the <property> tag and you could access values with key as a name.

I had to just recall that myself. The correct way of doing that is such

    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); 

You can instead of hardcoding username like I did temporarily there read it from your build.properties file. Have fun.

Dean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM