Skip to main content

Hey Fellows,

while I was deploying one of my applications to [OpenShift]( during the last days, I came across a little challenge:

My web application uses JPA with a datasource defined in a persistence.xml. This datasource has a name specific to my application to be able to deploy multiple applications to the same server instance and to connect each application to a different database (as it should be). So far so good.
At OpenShift I have got a JBoss 7 gear with a MySql gear. The wizard that creates the MySql gear configures the datasource name to be „_java:jboss/datasources/MysqlDS_“ no matter how the application is named. I also didn´t find a way to change the OpenShift configuration.
Minutes later I remembered that it is possible to set properties dynamically with Maven.

Here is the solution:


1: <?xml version=“1.0″ encoding=“UTF-8″ standalone=“no“?>
2: <persistence xmlns=““ xmlns:xsi=““ version=“2.0″ xsi:schemaLocation=““>
3: <persistence-unit name=“Sample_PU“ transaction-type=“JTA“>
4: <description>Sample Persistence Unit</description>
5: <provider>org.hibernate.ejb.HibernatePersistence</provider>
6: <jta-data-source>${datasource}</jta-data-source>
7: <exclude-unlisted-classes>false</exclude-unlisted-classes>
8: <properties>
9: <property name=““ value=“update“/>
10: <property name=“hibernate.show_sql“ value=“true“/>
11: <property name=“hibernate.format_sql“ value=“true“/>
12: <property name=“hibernate.transaction.flush_before_completion“ value=“true“/>
13: </properties>
14: </persistence-unit>
15: </persistence>
`Here the  tag specifies the datasource. I inserted a placeholder here. Then I created a _default_ Maven profile:


`1:  <profile>
    2:      <id>default</id>
    3:      <properties>
    4:          <datasource>java:jboss/datasources/Sample_DS</datasource>
    5:      </properties>
    6:  </profile>

And edited the existing _Openshift_ Maven profile:

`1:  <profile>
    2:      <id>openshift</id>
    3:      <properties>
    4:          <datasource>java:jboss/datasources/MysqlDS</datasource>
    5:      </properties>
    6:      <build>
    7:          <finalName>Sample</finalName>
    8:          <plugins>
    9:              <plugin>
    10:                  <artifactId>maven-war-plugin</artifactId>
    11:                  <version>2.1.1</version>
    12:                  <configuration>
    13:                      <outputDirectory>deployments</outputDirectory>
    14:                      <warName>ROOT</warName>
    15:                  </configuration>
    16:              </plugin>
    17:          </plugins>
    18:      </build>
    19:  </profile>

In the line 4 of the last two excerpts I defined the two different datasource names: _Sample_DS _and _MysqlDS._
_The last thing we have to do is tell Maven to replace all placeholders with the specified properties. To achieve this, we add the following snippet to the _build_ tag inside our POM:


`1:  <resources>
    2:      <resource>
    3:          <directory>src/main/resources</directory>
    4:          <filtering>true</filtering>
    5:      </resource>
    6:  </resources>

If you now run a Maven build, it will replace our _${datasource} _placeholder with the property defined in one of our Maven profiles.

Of course this is not limited to the persistence.xml file but to all files in the specified directory 😉
You can even override the property value on the command line by adding

`1:  -Ddatasource="java:jboss/datasources/ThirdDS"

to the build command!

Hope this has helped!

See you next time,


Daniel Sachse

Author Daniel Sachse

More posts by Daniel Sachse