If you create a new project with Netbeans and select to create “web application”.
You’ll notice that at ‘project properties’ – ‘Run’ there’s no ‘Working Directory’ configuration, unlike General Java Project.

But what if you want to create some console application (with main method) in that project with Netbeans. You run it with Netbeans shortcut shift+F6 (run file) and realize that Netbeans set the working directory to project base path.
Then you also try to run your unit testing (junit) and realize the same thing.
You put some configuration file to specific directory in your source folder, and of course your console app & unit testing can’t find it.

I’m not very familiar with Netbeans, but after some googling I found out that Netbeans using ant : file build-impl.xml in nbproject folder. If we change the project properties configuration Netbeans will re-generate the build-impl.xml.
Netbeans also provider file build.xml in project base folder for customizing project build & run.

So i add at build.xml (copy from build-impl.xml and modify the tag ‘dir’ value)

 <target name=”-init-macrodef-junit”>
<macrodef name=”junit” uri=”http://www.netbeans.org/ns/web-project/2″&gt;
<attribute name=”includes” default=”**/*Test.java”/>
<sequential>
<junit showoutput=”true” fork=”true” dir=”${basedir}/build/web/WEB-INF/classes” failureproperty=”tests.failed” errorproperty=”tests.failed”>
<batchtest todir=”${build.test.results.dir}”>
<fileset dir=”${test.src.dir}” includes=”@{includes}”/>
</batchtest>
<classpath>
<path path=”${run.test.classpath}”/>
</classpath>
<syspropertyset>
<propertyref prefix=”test-sys-prop.”/>
<mapper type=”glob” from=”test-sys-prop.*” to=”*”/>
</syspropertyset>
<formatter type=”brief” usefile=”false”/>
<formatter type=”xml”/>
</junit>
</sequential>
</macrodef>
</target>

<target name=”-init-macrodef-java”>
<macrodef name=”java” uri=”http://www.netbeans.org/ns/web-project/1″&gt;
<attribute name=”classname” default=”${main.class}”/>
<element name=”customize” optional=”true”/>
<sequential>
<java fork=”true” classname=”@{classname}” dir=”${basedir}/build/web/WEB-INF/classes”>
<jvmarg line=”${runmain.jvmargs}”/>
<classpath>
<path path=”${build.classes.dir.real}:${javac.classpath}:${j2ee.platform.classpath}”/>
</classpath>
<syspropertyset>
<propertyref prefix=”run-sys-prop.”/>
<mapper type=”glob” from=”run-sys-prop.*” to=”*”/>
</syspropertyset>
<customize/>
</java>
</sequential>
</macrodef>
</target>

So every unit test & run-file at Netbeans will have working directory to {project basedir}/build/web/WEB-INF/classes

Advertisements