Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

NetBeans Platform Tip: Adding New Resource Types to a JAR

DZone's Guide to

NetBeans Platform Tip: Adding New Resource Types to a JAR

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

The NetBeans Platform uses Ant for build processes.

To make changes for the process one can simply change the Ant file associated with the task.

For example, I want to include my resource file in the new JAR, so I open the "Compile" Ant task.

I can do it easily through the IDE's interface.

Here is the source of the "Compile" task for NetBeans Platform 6.1:

    <target name="compile" depends="init">
<mkdir dir="${build.classes.dir}"/>
<depend srcdir="${src.dir}" destdir="${build.classes.dir}" cache="build/depcache">
<classpath refid="cp"/>
</depend>
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="${build.compiler.debug}" debuglevel="${build.compiler.debuglevel}" encoding="UTF-8"
deprecation="${build.compiler.deprecation}" optimize="${build.compiler.optimize}" source="${javac.source}" target="${javac.target}" includeantruntime="false">
<classpath refid="cp"/>
<compilerarg line="${javac.compilerargs}"/>
</javac>
<!-- Sanity check: -->
<pathconvert pathsep=":" property="class.files.in.src">
<path>
<fileset dir="${src.dir}">
<include name="**/*.class"/>
</fileset>
</path>
</pathconvert>
<fail>
<condition>
<not>
<equals arg1="${class.files.in.src}" arg2=""/>
</not>
</condition>
You have stray *.class files in ${src.dir} which you must remove.
Probably you failed to clean your sources before updating them.
</fail>
<!-- OK, continue: -->
<copy todir="${build.classes.dir}">
<!-- #58298: strip comments to save some space -->
<fileset dir="${src.dir}" includes="**/*.properties"/>
<filterchain>
<tokenfilter>
<!-- #61965: preserve #NOI18N and similar comments -->
<filetokenizer/>
<replaceregex pattern="^#(?!(PART)?(NO)?I18N).*[\r\n]+" replace="" flags="gm"/>
</tokenfilter>
</filterchain>
</copy>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${jar-excludes}"/>
</copy>
</target>

You can see that the last step of the task is copying from source path to build path and line 31 does it for files with "properties" extension. To add a new extension ("XML", for example) I add a similar line:
<fileset dir="${src.dir}" includes="**/*.xml"/>

 

Changed task


After rebuilding the project, all my XML files will appear in the JAR and I can work with them as resources. Note however that the "Compile" task is a global task and that therefore this change will occur for all projects.

 

 

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}