Netbeans

Introduction

Netbeans (found here) deserves its own web page.  It’s a great tool, but sometimes as a developer it takes quite a while to understand the internals of the product. This page is dedicated to discussion some of those not-so-obvious features.

Using Filtering Effectively

One of the coolest features of Ant, the build system used by Netbeans, is that you can manipulate files in multiple ways in one foul swoop.  One such way is by using the <copy> target. <copy> has the ability to do global search and replace functions using the properties defined in the build at the time it’s called.  Specifically, you can get <copy> to replace everything in the form of

@my.property.name@

with the value of the specified property (in this case, my.property.name) while the file is being copied.  To accomplish this, just use the <copy> target like this:

        <copy todir="${my.destination.dir}" filtering="true">
            <fileset
                 dir="${source.dir}"
                 includes="the/path/to/a/properties/file/Resources.xml"
            />
        </copy>

Notice the use of the attribute

filtering="true"

this tells <copy> to replace the property names it finds between paired @ symbols with the value of the property of the same name.  The @ symbols are eliminated too.

This technique is very useful for setting up a single resource file that can be used by different Netbeans projects or different platform configurations for the same project.  Let’s say, for example, you want your application to know what kind of build that was specified (debug, info, warn, etc.).  You can have a resource file named above (Resources.xml) with the following information in it:

<application name="@application.name@"
             debuglevel="@debug.level@" />

When you copy the file using the <copy> target I mentioned above, the parameters application.name and debug.level will be filled in with the values Netbeans have set.  And, if you use that <copy> target in a Netbeans pre-defined utility target like <pre-jar>, you can get that resource file wrapped up into your distribution jar file:

<target name="pre-jar">
        <copy
              todir="${build.classes.dir}/path/in/the/jar/file"
              filtering="true">
            <fileset
                  dir="${source.dir}"
                  includes="the/path/to/a/properties/file/Resources.xml" />
        </copy>
</target>

With the build specific information in a resource file, your application can now respond to different settings related to the build.  In the above example, my program could adjust the logging to automatically correspond to the debug level without having to use a pre-processor.

And now it's your turn ... comment here or on Twitter (@Androider)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s