Super-simple way in Java with Netbeans to build all dependant libraries and subprojects with distribution

I think I finally figured out a simple and elegant way to build dependant libraries directly with the distribution and kiss that NoClassDefFound error goodbye. I also have a similar solution for Java Web Application projects.

What you need is:

Note that my solution is made for NetBeans 7.1.2 & Ant 1.8.3, so it may or may not work for older/newer versions.

What you need to do:

  1. Assign a single, shared libraries folder (Project properties -> Libraries -> Libraries folder) for all the projects in the solution. Make sure the external libraries are copied here.
  2. Add the following snippets in the /build.xml file in the projects of which you want to build dependencies:

If a normal Java Application:

<target name="-post-jar">
	<dirname file="${dist.jar.resolved}" property="dist.resolved.dir"/>
	<property name="dist.resolved.lib.dir" value="${dist.resolved.dir}\lib"/>

	<copy todir="${dist.resolved.lib.dir}">
		<fileset dir="${libraries.dir.nativedirsep}"></fileset>
	</copy>
</target>

If a Java Web Application:

<target name="-post-compile">
	<copy todir="${build.web.dir}/WEB-INF/lib">
		<fileset dir="${libraries.dir.nativedirsep}"></fileset>
	</copy>
</target>

You should have something like this now:

<project name="MyProject" default="default" basedir=".">
    <description>Builds, tests, and runs the project MyProject.</description>
    <import file="nbproject/build-impl.xml"/>

    <target name="-post-jar">
			<dirname file="${dist.jar.resolved}" property="dist.resolved.dir"/>
			<property name="dist.resolved.lib.dir" value="${dist.resolved.dir}\lib"/>

			<copy todir="${dist.resolved.lib.dir}">
				<fileset dir="${libraries.dir.nativedirsep}"></fileset>
			</copy>
    </target>
</project>

And that’s it.

How it works

We override an Ant target and simply copy the library files to the appropriate location when the project is built. So for projects that produces jar files the dependant libraries are moved to dist/lib, while for web projects the dependencies are copied to WEB-INF/lib before the .war is built.

Why does it not work when running, testing or debugging applications?

The answer lies within the build.xml file itself where the following is written:

<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->

Basically the build.xml file is only run when the project is cleaned or built by default.

To “fix” go to Project Properties and uncheck Compile On Save.

I’ll be trying to figure out if it’s possible to run build.xml on run, debug and test as well. If anyone already knows let me know!

This entry was posted in Java, Netbeans and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.