August 27, 2009

Copy Resources in NetBeans Java Project

When we need to copy resources in our Java project to the build directory, we have to extend the project's build.xml file. Suppose we have a default Java project in NetBeans. We create a new directory resources in the project directory. Here we place properties files, images or anything else we think is a resource and doesn't need to be compiled. We open build.xml and add the following ANT targets:

<target name="-post-compile-single" depends="-post-compile"/>

<target name="-post-compile">
    <copy todir="${build.classes.dir}">
        <fileset dir="resources/">
            <include name="**/**"/>

Anytime we compile a single source file, or the complete project, the files from the resources directory are copied to the build/classes directory.