Make Ant great again with NetBeans

My company NetBeans RCA is based on Apache Ant, Ant is a build tool for Java that was very popular when I started programming and many IDE used it to build. The NetBeans platform build system was totally based on Ant and this was very good because it allowed customizing the build process. We are currently using it to generate files to be included in the NBMs (the NetBeans Module), to update version numbers, to copy files, to upload files to our server …

Unfortunately, nowadays Ant lost popularity to Maven and Gradle which allows also to automatically download the necessary libraries, however, we have some many stuff built in Ant and we became experts at using it that it is not worth moving to Maven, that is currently also supported by the NetBeans Platform.

For those like me that are still using Ant, I would like to share some tips.

Install the Important Files plugin

If you create a new Java Application project (Not Maven) in NetBeans, the project is based on Ant, but you do not see its build script file unless you press CTRL+2 and search in the Files window. But, when you create a NetBeans Module project you get an Important Files node that shows all the configuration files used to build that module.

You can have the same node if you install a plugin called ImportantFiles 4 JavaSE. It will add a node with the build script and the properties files used to configure the module.

The build file will be listed as Build Script, Buildscript or build.xml .

How to launch easily  any Ant targets

Once you got access to the build file, if you expand it you will see the list of all available targets, you can run them by right-clicking on them and selecting Run Target. From this menu, you can also debug and inspect the targets by choosing Properties. The coolest feature is Create Shortcut… This action will create an item on the toolbar, on the menu or a keyboard shortcut to launch the task, it will only be visible when the project is open. I find it very convenient when I am working with my custom Ant targets that  I have to run often.

Another way to run an Ant Target is to right click on the build script node, choose Run Target and then the target you want to run. If the target you want to run is not present you might have to expand the Other Targets submenu. At the beginning, I was lost about why some targets appear at other targets and others don’t, then  I discovered it works like the -p Ant command line option: it lists only the targets that have a description. To add a description to a target, just add a description attribute with some text in it.

 

<target name="Hello World" description="It says Hello World" >
    <echo message="Hello World" />
</target>

How to customize the build process

When you open the build file there is a huge comment that explains how to customize the build process, in particular, it explains how to launch your own targets before or after any step in the build process.For instance, there are these two examples: one to add obfuscation after the build:

 

<target name="-post-compile">
    <obfuscate>
       <fileset dir="${build.classes.dir}"/>
    </obfuscate>
</target>

another to override the execution to launch your own executable

<target name="run" depends="AntApplication-impl.jar">
     <exec dir="bin" executable="launcher.exe">
          <arg file="${dist.jar}"/>
     </exec>
</target>

Hint for NetBeans Platform developers:  how to run the same target on all modules

It happened to me that I had to run the same target on all my modules in a suite. You can do it in this way:

Create a new Target that depends from -init and use subant 

<subant buildpath="${modules.sorted}" target="spotbugs" inheritall="false" inheritrefs="fakse" failonerror="false" />
Photo by Mikhail Vasilyev on Unsplash

Leave a Comment

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