Netbeans - adding resource files to jar file with Ant

10,381

Solution 1

The answer I think I was looking for is as follows:

In the build.xml file (as per trashgod's answer) you can use the hooks already in the ant build script to add the following:

<target name="-post-jar">
    <echo>Adding files to jar...</echo>
    <jar destfile="dist/jarFileName.jar" update="true">
        <fileset dir="${basedir}">
            <include name="files/*"/>
        </fileset>
    </jar>
</target>

This adds the files directory and any files under it directly to the jar file.

Solution 2

If you choose File > Project Properties > Build > Packaging, you'll see a dialog that lets you exclude artifacts from the build; everything else is the source tree is included. The source of TreeIconDemo is a concrete example that inlcudes html files.

For more advanced tasks, examine the default build.xml generated for a freshly created project; it identifies various hooks into the predefined tasks. For example,

There exist several targets which are by default empty and which can be 
used for execution of your tasks. These targets are usually executed 
before and after some main targets. They are: 

  -pre-init:                 called before initialization of project properties
  -post-init:                called after initialization of project properties
  -pre-compile:              called before javac compilation
  -post-compile:             called after javac compilation
  -pre-compile-single:       called before javac compilation of single file
  -post-compile-single:      called after javac compilation of single file
  -pre-compile-test:         called before javac compilation of JUnit tests
  -post-compile-test:        called after javac compilation of JUnit tests
  -pre-compile-test-single:  called before javac compilation of single JUnit test
  -post-compile-test-single: called after javac compilation of single JUunit test
  -pre-jar:                  called before JAR building
  -post-jar:                 called after JAR building
  -post-clean:               called after cleaning build products

Addendum: As an example, this target overrides -post-compile to print some statistics.

<project name="TreeIconDemo" default="default" basedir=".">
    <import file="nbproject/build-impl.xml"/>
    <target name="-post-compile">
        <echo>build.dir: ${build.dir}</echo>
        <length mode="all" property="build.size">
            <fileset dir="${build.dir}">
              <include name="**/*"/>
            </fileset>
        </length>
        <echo>build.size: ${build.size}</echo>
    </target>
</project>

Output:

$ ant compile
Buildfile: build.xml
...
-post-compile:
     [echo] build.dir: build
     [echo] build.size: 11992

compile:

BUILD SUCCESSFUL
Share:
10,381
Alistair Collins
Author by

Alistair Collins

Wet Gas Metering Specialist at Solartron ISA, as well as currently taking a Flow Measurement and Fluid Mechanics EngD, with a little bit of time for playing in Go and other programming languages... ..although with a wife, two kids, playing keyboards at church etc, it's a fun and busy life!!

Updated on June 04, 2022

Comments

  • Alistair Collins
    Alistair Collins almost 2 years

    I want add some resource files (non-code text-based files) to the jar file in a Java project using Netbeans 6.9, I'd expect using Ant. I had thought that this would be reasonably simple...but after quite a bit of searching I can't find how to do it..! Any pointers in the right direction?