Over a million developers have joined DZone.

NetBeans Platform Tip: Adding New Resource Types to a JAR

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

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"/>
<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}"/>
<!-- Sanity check: -->
<pathconvert pathsep=":" property="class.files.in.src">
<fileset dir="${src.dir}">
<include name="**/*.class"/>
<equals arg1="${class.files.in.src}" arg2=""/>
You have stray *.class files in ${src.dir} which you must remove.
Probably you failed to clean your sources before updating them.
<!-- OK, continue: -->
<copy todir="${build.classes.dir}">
<!-- #58298: strip comments to save some space -->
<fileset dir="${src.dir}" includes="**/*.properties"/>
<!-- #61965: preserve #NOI18N and similar comments -->
<replaceregex pattern="^#(?!(PART)?(NO)?I18N).*[\r\n]+" replace="" flags="gm"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${jar-excludes}"/>

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.



Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}