Skip to content

Instantly share code, notes, and snippets.

@edro15
Created November 19, 2019 10:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edro15/000d55c38efc66bf05f584880f63852b to your computer and use it in GitHub Desktop.
Save edro15/000d55c38efc66bf05f584880f63852b to your computer and use it in GitHub Desktop.
[How To] Compile a Java NetBeans project with Apache Ant

Problem

You need to modify code of a Java NetBeans project but you neither have or want to install that IDE in order to re-compile it.

Solution

Use Apache Ant to compile your Java code.

Instructions

  • Create a folder (e.g. lib) in your project, at the same level of nbproject, and save all dependencies (jars) there.

    ~$ cd /path/to/your/app && mkdir lib && cp /path/to/jars lib/

  • Save there also org-netbeans-modules-java-j2seproject-copylibstask.jar. This jar is required to execute NetBeans build.xml.

    • Where do I find this jar?

      Its path can be extracted from build.properties in folder nbproject

    • What if not available there?

      If you had an old version of NetBeans installed, check .netbeans/<version>/build.properties

  • Create a wrapper (e.g. build-ext.xml) for NetBeans build.xml, at the same level of nbproject, to overwrite properties from the environment variables.

    For example ${platforms.nameofyourplatform.home} was defined either in nbproject/build-impl.xml or nbproject/build.properties as platforms.java

  • Fill in the wrapper with something similar to the following

<?xml version="1.0"?>
<project name="BUILD-EXT" default="build-ext" basedir=".">
    <import file="build.xml"/>
    <property environment="env"/>
    <property name="platforms.java" value="${env.JAVA_HOME}" />
    <target name="build-ext" depends="default"/>
</project>
  • Run ant by executing ~$ ant -Dlibs.CopyLibs.classpath=lib/org-netbeans-modules-java-j2seproject-copylibstask.jar -f build-ext.xml
    • Got error copylibs doesn't support the "excludeFromCopy" attribute?

      At line XX (specified in the error) of build-impl.xml remove setting excludeFromCopy, save and run ant command again

Sources and research material

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment