views:

23

answers:

2

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?

+1  A: 

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
trashgod
Thanks trashgod. I had seen the hooks into the build process...but I'm not certain how to use them, and am suitably confused by the different websites and blogs etc! Could you provide an example of how to use a suitable hook to include the file data.txt in directory fileDir under the root in the jar file in the same location?
Alistair Collins
@Alistair Collins: I've elaborated above.
trashgod
A: 

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.

Alistair Collins