(Netbeans 6.1, external ant 1.7 )

If you create a standalone Java application project (i.e. one that comes with all used external jars and libs included, so there are copied to dist/lib) in Netbeans, commit the changes to version control, checkout or update the changes to your test and integration server and try to build the same project there, you might run into problems even if you used an external ant for building the same project in Netbeans on your local machine. (Look here for an explanation why jars are not includeable in the distribution jar itself.)

On the integration server the libs will not be copied to the dist/lib folder. The reason is that Netbeans is using an own ant task org.netbeans.modules.java.j2seproject.copylibstask which extends import org.apache.tools.ant.taskdefs.Jar and this jar will just not be found on the integration server except you completly reconstruct your local build environment on the integration server, what will be difficult if you are using a CI system (in my case it’s hudson). So let’s have a closer look at this issue and some possible solutions:

There are two principal ant build files Netbeans is using during the build process: build.xml – user can edit this file, build-impl.xml – user should not edit this one. Also have a look at this post Visualizing Default Ant Task Dependencies for Netbeans build-impl.xml.

Where and when the Libs are copied to dist/lib by the CopyLibs task?

For that we have to look into build-impl.xml. The libs are copied when the jar target ist executed:

 <target 
depends="init,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar"
description="Build JAR." 
name="jar"/>

There are several jar related tasks, which are executed when certain conditions are valid, that means certain variables are set. The interesting task here is -do-jar-with-libraries:

 <target depends="init,compile,-pre-pre-jar,-pre-jar" 
if="manifest.available+main.class+mkdist.available" 
name="-do-jar-with-libraries">

This task will be executed if property manifest.available+main.class+mkdist.available is set. Especially if the path to CopyLibs is set.

        <condition property="manifest.available+main.class+mkdist.available">
            <and>
                <istrue value="${manifest.available+main.class}"/>
                <isset property="libs.CopyLibs.classpath"/>
            </and>
        </condition>

Where does this path comes from. It is read in

    <target depends="-pre-init,-init-private,-init-libraries" name="-init-user">
        <property file="${user.properties.file}"/>

For example from: .netbeans/6.1/build.properties.

libs.CopyLibs.classpath=/home/user/bin/netbeans/java2/ant/extra/org-netbeans-modules-java-j2seproject-copylibstask.jar
libs.CopyLibs.javadoc=
libs.CopyLibs.maven-pom=
libs.CopyLibs.src=

That’s a problem! The build.properties file will not be found on the integration server, the properties libs.CopyLIbs.classpath and manifest.available+main.class+mkdist.available will not be set, the task -do-jar-with-libraries will not be executed.

What do you need CopyLibs task for?

Look at how the task is used (I included some echos for better understanding):

<target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <echo message="-------------run.classpath------- ${run.classpath}"/><echo message="--------- build.classes.dir.resolved ------ ${build.classes.dir.resolved}"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
            <path path="${run.classpath}"/>
            <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <echo message="-------- run.classpath.without.build.classes.dir ---- ${run.classpath.without.build.classes.dir}"/>
        <pathconvert pathsep=" " property="jar.classpath">
 
            <path path="${run.classpath.without.build.classes.dir}"/>
            <chainedmapper>
                <flattenmapper/> <target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
                <globmapper from="*" to="lib/*"/>
            </chainedmapper>
        </pathconvert>
        <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
        <copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
            <fileset dir="${build.classes.dir}"/>
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
                <attribute name="Class-Path" value="${jar.classpath}"/>
            </manifest>
        </copylibs>
        <echo>To run this application from the command line without Ant, try:</echo>
        <property location="${dist.jar}" name="dist.jar.resolved"/>
        <echo>java -jar "${dist.jar.resolved}"</echo>
    </target>

and then study the java code. So not much problems here. Mainly copying some files and using standard jar functions, also providing some README file. Below I will show how you can workaround using this task.

Solutions?

First, you could copy the CopyLibs Task jar somewhere and set the property libs.CopyLibs.classpath in your build.xml to the appropriate location or use project.properties file which comes with the Netbeans build scripts. There are little chances that this task will change and you will have to update the CopyLibs Task jar.

Second, you could build your standalone application using One-JAR overriding the jar target from build-impl.xml.

Third, don’t change build structure but avoid using CopyLibs task. Here is an example how to change build.xml:

First make macrodefs and presetdefs from build-impl.xml callable by declaring appropriate namespaces in build.xml:

<project name="Test" default="default" basedir="."
xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1"
xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
    <description>Builds, tests, and runs the project Test.</description>

Then redefine the jar target a little and override it in build.xml (echos only for feedback):

<!-- target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries" -->
     <target depends="init,compile,-pre-jar,-post-jar" description="Build JAR." name="jar">
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <echo message="-------------run.classpath------- ${run.classpath}"/>
        <echo message="--------- build.classes.dir.resolved ------ ${build.classes.dir.resolved}"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
            <path path="${run.classpath}"/>
            <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <echo message="-------- run.classpath.without.build.classes.dir ---- ${run.classpath.without.build.classes.dir}"/>
        <pathconvert pathsep=" " property="jar.classpath">
            <path path="${run.classpath.without.build.classes.dir}"/>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*" to="lib/*"/>
            </chainedmapper>
        </pathconvert>
        <echo message="--- build.classes.dir --- ${build.classes.dir}" />
        <copy todir="${dist.dir}/lib" flatten="true">
            <path>
                <pathelement path="${run.classpath.without.build.classes.dir}"/>
            </path>
        </copy>
        <j2seproject1:jar manifest="${manifest.file}">
            <j2seproject1:manifest>
                <j2seproject1:attribute name="Main-Class" value="${main.class}"/>
                <j2seproject1:attribute name="Class-Path" value="${jar.classpath}"/>
                <!-- if you deal with versions -->
                <!-- j2seproject1:attribute name="Build-Version" value="${version}" / -->
            </j2seproject1:manifest>
        </j2seproject1:jar>
        <echo>To run this application from the command line without Ant, try:</echo>
        <property location="${dist.jar}" name="dist.jar.resolved"/>
        <echo>java -jar "${dist.jar.resolved}"</echo>
    </target>

… and you’ve got rid of the annoying CopyLibs task thing.