Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 307
santiagozky Avatar asked May 21 '12 14:05

santiagozky


People also ask

How do you specify data source in persistence xml?

xml , choose the Source tab. Specify the data source for the persistence unit. Typically, you use the relevant data source alias name to specify the DataSource. Make sure that the value of the jta-data-source tag is the same as the value of the alias tag in the data-source-aliases.

How do I edit persistence xml?

Procedure. In the Package Explorer view, right-click the persistence. xml file of the JPA project that you want to edit and select Open with > Persistence XML Editor. In the Design tab of the Persistence XML Editor, make any of the following changes to the persistence.

What is the purpose of the persistence xml file?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).

Can we have multiple persistence xml?

xml files. Have multiple persistence units in one persistence.


1 Answers

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.

like image 171
khmarbaise Avatar answered Sep 21 '22 07:09

khmarbaise