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.

Left Over Ghost Breakpoints

If you are using the same library jar files with source attachements in different NetBeans projects, it could happen to you that breakpoints you set in one project magically appear in an other project, too. Unfortunately they are not deletable there, only in the project where they were set originally.

Often I have to switch between many projects back and forth. Then getting rid of these “ghost” breakpoints proved difficult. Here is what to do:

On windows, NetBeans stores workspace configuration data in your windows home directory under AppData/Roaming/NetBeans/version-number. In subdirectory config/Services is the file “org-netbeans-modules-debugger-Settings.properties” located. Breakpoints are stored in this file. After deleting them there, you definitely get rid of them!

My NetBeans Configuration

October 2013 was the last month I used Eclipse Kepler. After years the bumpy maven support was reason enough for me to have a look again at NetBeans. When I first started out to develop programs in Java, there was no free IDE available, except Forte for Java, which is the predecessor of todays NetBeans. I used it back then, but with the emerging of Eclipse, my employer made it’s usage mandatory for all programmers.
Todays NetBeans has nothing in common with the slow and clumsy Forte for Java. NetBeans now is fast, responsive and elegant. The flawless maven support of NetBeans was the first feature that got me excited about this IDE. My actual work environment allows me to choose between Eclipse or NetBeans. Currently I am the only one who is using NetBeans for the complete development lifecycle, but interest is growing and already some co-workers are using at least matisse to design their java swing frames.
Checkstyle usage and an Eclipse Formatter Defintion are mandatory for everyone. Unfortunately no guidelines are given, how to make NetBeans compliant with our checkstyle and formatting rules. Not a big issue with Eclipse, but some thinking is to be done, when using NetBeans to make it compliant with my companies standards.

Here is what I did:

Checkstyle Support
The “Software Quality Environment” provides support for most code checkers and does it’s job very well. Have a look at it here: https://kenai.com/projects/sqe/pages/Home.

Eclipse Formatter
Surprisingly there is also a NetBeans plugin available, that does the trick: formatting of source files according to rules specified in an eclipse formatter definition file. Check it out here: http://benkiew.wordpress.com/2013/11/14/new-version-of-java-eclipse-code-formatter-plugin-available/.

Explore from here
Nice little plugin, that starts the Microsoft Explorer with your projects directory selected. You may select it’s menu entry from right clicking on the NetBeans project root node.

License Changer
Ever had to change the licenses in your source files header? This plugin comes to the rescue:
http://plugins.netbeans.org/plugin/17960/license-changer

Here you will find the highlights of NetBeans keyboard shortcuts and code templates:
https://netbeans.org/project_downloads/usersguide/shortcuts-74.pdf

Other Settings

NetBeans default font is too small for my eyes. The font size can be set in <home>/etc/netbeans.conf. Add “--fontsize 13” to “netbeans_default_option”. Change the font size to a value that suits you.

Choose Tools -> Options -> Java -> Maven and set “Skip Tests for any build executions not directly related to testing”. This makes sure that your tests are no executed when building your project.

Choose Tools -> Options -> Editor -> Formatting. Select Language “Java” and Category “Comments” and deselect “Enable Comments Formatting”. The comments formatting regularly removes blanks from my thoroughly designed license header in source code files. Therefore I deselect it.

Choose Tools -> Options -> Editor -> Macros. Enter new macro name “join-line”, shortcut Ctrl+J, macro code is:
caret-end-line selection-down selection-first-non-white remove-selection