Amazing NetBeans JAR Vanishing Act

Today I encountered a bizarre and obscure misbehavior of NetBeans 7.3.1 that you should be aware of when writing JavaFX applications. The situation: I wanted to add some text files to a JavaFX project that should be copied to the output folder at every build.

The NetBeans way to do this is to add a copy target to the placeholder file build.xml which resides in the top directory of every NetBeans project. Which target should we use? Why, the target -post-jar of course which is the first Google hit and also recommended in the NetBeans standard build file build-impl.xml:

<target depends="-jfx-copylibs,-rebase-libs,jfx-deployment" name="-post-jar">
    <!-- Empty placeholder for easier customization. -->
    <!-- You can override this target in the ../build.xml file. -->
</target>

Right, so let’s edit build.xml to copy our ReadMe file to the output folder!

<target name="-post-jar">
    <copy file="ReadMe.html" flatten="true" todir="${dist.dir}"/>
</target>

And verily, the ReadMe file was copied to the output folder as intended… but now the JAR file had vanished! After a full Clean & Build, NetBeans would correctly compile and copy everything else, but would no longer generate the executable JAR file. The Ant build output showed no error messages, so how could that happen?

After hours of tweaking settings and deleting caches and even recreating the entire project, I eventually realized that NetBeans’ build.xml file for my JavaFX application actually did not list -post-jar as a valid override target. Turns out that the new JavaFX build script employed by NetBeans requires the new override target -post-jfx-jar instead – and this script is so brittle that overriding the old target skips the entire JAR build process!

I don’t know enough about Ant to figure out exactly what’s going on here, but I could reliably reproduce that the above copy operation will suppress JAR file creation, whereas replacing -post-jar with -post-jfx-jar will create the JAR file as expected. The JavaFX build system introduces a variety of other pre-jfx- and post-jfx- placeholder targets, by the way – see the comments in build.xml for details.

Leave a Reply