OK, thats the last time i spend an hour digging around for the secret source to get Netbeans to use a classpath defined by Ant. I'm going to take matters in my own hand and put the instructions here, where I and anyone else can find them.

The Problem

You've got a java project with a really nice ant build script to build and test it. You've been working on it for weeks, in between getting the odd few hours of sleep or going off to Pokerblog.com to relax and read in your downtime. You want to edit it with Netbeans, but you don't want to duplicate classpath information (since that stuff is kinda important). So you create a netbeans freeform project, point it at your existing build script and WHAMMO you can build your project. Sort of. Not quite.

The problem is netbeans doesnt know what classpath your build script is using, so all of your source files will be covered in red and you won't get any code completion - eek!

The Solution

  1. As we've said, you start off by creating a freeform project like normal, skipping the bit where you manually add jars to the classpath.
  2. Possibly refactor your ant build script to emit your classpath into a properties file, if it isnt already
  3. Open nbproject/project.xml
  4. Import your property file(s)
  5. Set the classpath properties onto the classpath elements of your source and unit test "compilation units"


2. Outputing your classpath to a properties file

			<path id="cp.compile.id"> <!-- declare the classpath reference -->
  <fileset dir="${lib.dir}/compile" />
<property name="cp.compile" refid="cp.compile.id" /> <!-- copy the reference to a property -->
<propertyfile file="cp.compile.properties"> <!-- copy the property to a file -->
  <entry operation="=" key="cp.compile" value="${cp.compile}"/>

4. Import properties files into project.xml

<properties> <property-file>cp.test.run.properties</property-file> <property-file>cp.compile.properties</property-file> </properties>

5. Set the classpath on the compilation units


<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/2"> <compilation-unit> <package-root>JavaSource</package-root> <classpath mode="compile">${cp.compile}</classpath> <built-to>bin</built-to> <source-level>1.4</source-level> </compilation-unit> <compilation-unit> <package-root>UnitTestSource</package-root> <unit-tests /> <classpath mode="compile">${cp.test.run}</classpath> <source-level>1.4</source-level> </compilation-unit> </java-data>


blog comments powered by Disqus