can I inject values into the persistence.xml file from maven?
Asked Answered
C

1

8

I want to be able to store my database information in a pom.xml (as properties) and inject the necessary values into my persistence.xml file. Is there any way I can achieve this in maven?

an alternative would be how to keep my database connection information in one file and be able to feed it to both my pom.xml and my persistence.xml

Comedown answered 21/5, 2012 at 14:26 Comment(0)
J
12

You can locate your persistence.xml into a a location like src/main/resources/PATH and use the filtering option to filter your persistence.xml and put into the correct location. This can be achieved by activating the filtering in resources like this:

<resource>
  <directory>src/main/resources/PATH</directory>
  <filtering>true</filtering>
</resource>

The same for your test resources:

<testResources>
  <testResource>
    <directory>src/main/resources/PATH</directory>
    <filtering>true</filtering>
  </testResource>
</testResources>

Based on the above you can give things like this in your persistence.xml

   <hibernate.url>${database.url}</hibernate.url>

What you need to check is the correct target location of the persistence.xml file (i can remember something like META-INF/.. ? If yes that put it into src/main/resources/META-INF and change the filter directory accordingly.

Jillian answered 21/5, 2012 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.