This is possible, though without changing the persistence.xml
:
We use maven profiles and resource filtering for that. You will need to define placeholders in your persistence.xml
that match the property names in your .properties
file or in your .pom
.
During the build, you specify the profile and maven will replace the placeholders with your properties.
We have used this technique for switching the datasource between different deployment environments. You can also use it for switching PUs or other properties.
First, define a profile for resource filtering:
<profiles>
<profile>
<id>set_datasource</id>
<build>
<!-- enable resource filter to set the datasource name -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
...
Create a profile for each datasource:
<profile>
<id>db_test</id>
<properties>
<database.name>test_ds</database.name>
</properties>
</profile>
In your persistence unit, prepare the placeholder
<persistence-unit name="my_db">
<jta-data-source>java:jboss/datasources/${datasource.name}</jta-data-source>
</persistence-unit>
Call maven with the two profiles:
mvn test -Pset_datasource,db_test
Please note, we use this mainly for UI- and user-tests. For integration/feature tests we use Arquillian. For Arquillian, you can define a separate persistence.xml
file, or even create one on-the-fly.