NetBeans and Maven Profiles

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:

  • ROOT
    • env
      • dev
      • int
      • prod
    • src
      • main
        • java
        • resources
      • test
    • target

Underneath the env directory are three subdirectories, one for each environment. They contain the relevant persistence.xml files.

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.

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>dev</build.profile.id>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>
                            env/${build.profile.id}
                        </directory>
                    </resource>
                    <resource>
                        <directory>
                            src/main/resources
                        </directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>int</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>int</build.profile.id>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>
                            env/${build.profile.id}
                        </directory>
                    </resource>
                    <resource>
                        <directory>
                            src/main/resources
                        </directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>prod</build.profile.id>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>
                            env/${build.profile.id}
                        </directory>
                    </resource>
                    <resource>
                        <directory>
                            src/main/resources
                        </directory>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>

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:

setconfiguration1

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”:

dev1

After selecting the “int” environment (which is in my organization also used as acceptance test), NetBeans links to the “env/int” folder:

int

Pretty cool! This way you can easily switch between Maven profiles, edit the profile related resources and build your artifact.

Leave a Reply

Your email address will not be published. Required fields are marked *