Quite often it is needed to build an artifact for different environments. For instance: I need to deploy an ejb-jar file that contains a
persistence.xml file, that links it’s persistence units to datasources via JNDI entries. The data sources are in each environment (development, acceptance test and production) different and configured in my case within a RedHat JBoss Application Server. This is usually done by the application management or server administrators. All that is needed to know by the development team is the JNDI name the persistence unit refers to.
So far there would be no need for different
persistence.xml files. Unfortunately in the development and acceptance test environments additional databases are available for complex testing scenarios. Also multi tenancy is implemented in a way, that each tenant has it’s own database. This alone leads to different numbers of databases in each environment and hence to different numbers of persistence units and an environment dependend
persistence.xml. To meet this requirement, I build one ejb-jar file for each environment with a specific
persistence.xml file in the META-INF directory.
The project directory layout is:
env directory are three subdirectories, one for each environment. They contain the relevant
Here is where the maven profiles come in: for each environment I set up an own profile, that joins in the respective environment directory as a resource directory.
Since NetBeans uses maven to organize it’s project structure, it works very nicely with maven profiles. Via a right mouse click at the project root in the “Projects” tab and selecting “Set Configuration” you are able to choose the current maven profile:
The really cool thing is: depending on the profile you chose, NetBeans shows under “Other Sources” the profile related resource directory. In this case I selected the “dev” environment and NetBeans links to the folder “env/dev”:
After selecting the “int” environment (which is in my organization also used as acceptance test), NetBeans links to the “env/int” folder:
Pretty cool! This way you can easily switch between Maven profiles, edit the profile related resources and build your artifact.