FaqSpecialJar

How can I customize what goes into my project's JAR file?

By default, a plain Java project when built will create a JAR containing just

  1. All compiled classes from Source Packages.
  2. All other resource files from Source Packages. More specifically, everything not listed in the Exclude from JAR File field in the Packaging panel of the project customizer.
  3. A manifest, if you have one (by default only for "application" projects).

Usually if you want to include extra resources in your JAR, you should just put them into Source Packages. (You can add additional source package folders to the project, for example to keep resources in a separate file tree. These classes or resources will still be packed into the same JAR according to Java package structure.)

In some cases you might want to include extra files which for whatever reason you do not wish to keep in Source Packages, or to exclude some files which would otherwise be included, or to move some files, etc. You can do all of these things by creating special Ant targets in your build.xml. As an example, let's say you wanted to pack a list of new features into your JAR, but you want to generate the feature list using XSLT. Let's create a file whatsnew.xml in the main project folder (visible under the Files tab):

<?xml version="1.0" encoding="UTF-8"?>
<whatsnew>
    <item>
        New feature #1
    </item>
    <item>
        New feature #2, <em>really cool</em>
    </item>
</whatsnew>

Now we need a stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>What's New</title>
            </head>
            <body>
                <h1>What's New in My Application?</h1>
                <ol>
                    <xsl:for-each select="//item">
                        <li>
                            <xsl:copy-of select="node()"/>
                        </li>
                    </xsl:for-each>
                </ol>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Finally let's create the HTML entry in the JAR. Add to your build.xml the following:

    <target name="-pre-jar">
        <xslt in="whatsnew.xml" out="${build.classes.dir}/whatsnew.html" style="whatsnew.xsl"/>
    </target>

Now when you build your project the JAR should contain a whatsnew.html at the top level.

For another example, let's say you have a source tree which contains both a library, in the package framework (and subpackages), and some examples, in the package examples (and subpackages). Normally you would make two projects out of this (with the examples project having a dependency on the framework project), which would not require any special build logic and would also prevent the framework code from accidentally referring to the examples; but let's say that for whatever reason you really want this all in one project and all in one source tree. You just need to add to your build.xml:

    <target name="-init-presetdef-jar" xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1">
        <presetdef name="jar" uri="http://www.netbeans.org/ns/j2se-project/1">
            <jar jarfile="${dist.jar}" compress="${jar.compress}">
                <j2seproject1:fileset dir="${build.classes.dir}">
                    <exclude name="examples/"/>
                </j2seproject1:fileset>
            </jar>
        </presetdef>
    </target>

Now you can still run examples with Shift-F6, and building the project compiles all the examples; but your project's JAR will contain only the framework, not the examples.

Alternately, you can click on Includes/Excludes in the Sources tab of your project's Properties dialog, and add examples/ to Excludes. Now the examples package will be hidden in Projects (you can still see the files in Files). Sources in this package will not be compiled or packaged, and you cannot accidentally refer to them in framework sources.


Applies to: NetBeans 6.0 and later

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo