2

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?

2 Answers2

0

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.

Phani
  • 5,319
  • 6
  • 35
  • 43
  • how could I inject values of the build.properties to persistence.xml? These link are not showing persistence.xml. I need to get the values into persistence.xml from build.properties while the build.xml is running....that means values should reflex in persistence.xml automatically... – user1340856 Apr 24 '12 at 07:29
  • Please go through the Replace values with Ant Task link for doing that. – Phani Apr 24 '12 at 07:32
  • Sorry..I didn't get any idea from this link. What this tag actually do? – user1340856 Apr 24 '12 at 07:39
  • for example you have a value which needs to be replaced at build time? @name@. And if you define name=X bean in build.properties file. Then replace tag will replace @name@ with X at the time of build. – Phani Apr 24 '12 at 09:00
  • Actually in my code, build.proerties contains username=uname, password=pwd. I need to get automatically reflected this values under tag in persistence.xml(like while build.xml is running...what all tags I need to write in build.xml. urgent requirement..please help.. – user1340856 Apr 25 '12 at 06:53
  • This is the 100% worst answer as generally the persistence.xml is in the jar file and on a QA or production server, you modify the build.properties on that machine and QA can't modify the persistence.xml file because it is in the jar file and QA should not have ant. – Dean Hiller Sep 07 '12 at 14:55
  • FYI, this is run on a build system which modifies the configuration of the application based on the build type(QA/PROD) then the configured WAR/EAR moved to respective environment. And one more thing, in question it's not mentioned on QA/PROD. – Phani Nov 09 '12 at 10:29
0

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

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212