santiagozky
santiagozky

Reputation: 2539

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

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

Upvotes: 8

Views: 5394

Answers (1)

khmarbaise
khmarbaise

Reputation: 97447

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.

Upvotes: 12

Related Questions