Hi there,
I want to share another challenge in my job and how I overcome it. First of all, let me explain my challenge. There are several submodules in my project but some modules which are data model modules should be able to deploy different environment such as Glassfish application server or Spring boot tomcat server. As you know there is a persistence.xml
in data model modules that we can define data source resource. However, you may change these data source name with respect to environment. This is my challenge, I didn't know how to figure it out. Finally I have just found it and I want to share it in here to not lose the solution. Here solution:
First of all you need to add these profiles in your pom.xml
<profiles>
<profile>
<id>set_datasource</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>glassfish_deploy</id>
<properties>
<datasource.name>datasourceName</datasource.name>
</properties>
</profile>
<profile>
<id>spring_deploy</id>
<properties>
<datasource.name>java:comp/env/datasourceName</datasource.name>
</properties>
</profile>
</profiles>
Then you need to update your persistence.xml
to replace data source name with the variable name you declare above.
<persistence-unit name="testpu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>${datasource.name}</jta-data-source>
.
.
</persistence-unit>
Finally you can build this module with respect to environment you want to deploy in. For glassfish:
mvn clean install -Pset_datasource,glassfish_deploy
For spring boot
mvn clean install -Pset_datasource,spring_deploy
Special thanks to kostja for his answer in this stackoverflow question it helped me a lot.