Hey Fellows,
while I was deploying one of my applications to [OpenShift](http://openshift.redhat.com/) 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:
**src/main/resources/META-INF:**
1: <?xml version=“1.0″ encoding=“UTF-8″ standalone=“no“?>
2: <persistence xmlns=“http://java.sun.com/xml/ns/persistence“ xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“ version=“2.0″ xsi:schemaLocation=“http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“>
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=“hibernate.hbm2ddl.auto“ 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
**pom.xml:**
`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:
**
****pom.xml:**
`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:
**pom.xml:**
`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,
w0mbat