NetBeans, Ant, and Jenkins

Getting a NetBeans project with an Ant buildfile to build on Jenkins (or Hudson, and likely many other CI systems) is a pain.  I’m not going to waste time chatting here, lets get straight to the recipe

System-Wide Configuration

This part only needs doing once for the Jenkins server (although if you have multiple nodes, you’ll probably have to do it on each one)

You’ll need Ant 1.9.4 – My Ubuntu server has 1.9.3 so I told Jenkins to install its own copy of 1.9.4 from Apache (Manage Jenkins>Configure System)

You’ll need a copy of the relevant JDK required by your project. I put mine in /opt/jdk1.8.0_45

You’ll also need a copy of the java (and possibly javafx, if you’re using that) folders from a NetBeans install for access to various libraries. I put these into /opt/NetBeans8 (you could alternatively just install a full copy of NetBeans on the server, but mine is headless and only needed the libraries.

You’ll need a build.properties file.  To find this, open nbproject/private/private.properties file from one of your NetBeans projects (you may have to run a local build first to generate it) and look for the user.properties.file setting – then go grab the file it points at.

You’ll need to run a search/replace (or a manual edit) on that file to change every reference to the JDKs on your local machine and to folders from the local NetBeans install to point at the ones on your server (/opt/jdkxxxx and /opt/NetBeans8 in my case.)  It may take a little while to edit, but once it’s done it’s done.

Now shove that edited build.properties into an accessible location on the Jenkins server (/opt/NetBeans8 for me, although you could alternatively put it into the Jenkins user’s home directory)

Per-Project Configuration

This part you’ll need to do once per project.  Part needs doing in the Jenkins project configuration and part on your NetBeans project.

In Jenkins (go to the Configure screen for your project) set up an “Invoke Ant” build step, using the version of Ant you specified earlier.

Go into Advanced settings for that Ant task, and set the “Build File” to

jenkins-build.xml

Now over to your NetBeans project.

Add a new Ant build file in the root directory of your project, and name it jenkins-build.xml

This file is going to be a wrapper around Ant’s standard build.xml, and will “fill in the blanks” that NetBeans usually does for you when you build from within the IDE (mostly a case of having a private.properties file that tells Ant where to find the build.properties file, but it also needs to include any  references to other projects.)  Here’s the file for my “Kestrel Stub” project for you to use as a template – obviously you’ll need to edit the path and file names.  Note that you’ll need to add some lines if your project references other projects – I’ve added an example in the comments.

<?xml version="1.0" encoding="UTF-8"?>
<!--- Ant "preloader" script for running on Jenkins -->
<project name="Kestrel Stub on Jenkins" default="build">
    <!-- create private folders -->
    <mkdir dir="nbproject/private"/>
    <!-- You'll also need this for each referenced project. For example: -->
    <!-- mkdir dir="../kestrel-utils/nbproject/private"/-->
    <!--- set variables needed by Ant when outside of Netbeans -->
    <propertyfile file="nbproject/private/private.properties">
        <entry key="user.properties.file" value="/opt/NetBeans8/build.properties"/>
        <!-- Add any project references here -->
        <!--entry key="reference.kestrel-utils.jar" value="${basedir}/../kestrel-utils/dist/kestrel-utils.jar"/-->
        <!--entry key="project.kestrel-utils" value="${basedir}/../kestrel-utils"/-->
    </propertyfile>
    <!-- If you have any referenced projects, you'll need to set their 
         private.properties here too.  Again, here's a-->
    <!--propertyfile file="../kestrel-utils/nbproject/private/private.properties">
        <entry key="user.properties.file" value="/opt/NetBeans8/build.properties"/>
    </propertyfile-->
    <!-- Build targets - these just chain a call to the original build file-->
    <target name="build">
            <ant dir="${basedir}" target="build"/>
    </target>
    <target name="clean">
            <ant dir="${basedir}" target="clean"/>
    </target>
</project>

Note that this handles “build” and “clean” targets. If you want Jenkins to be able to do any others, just add the appropriate target sections at the end.

 

Leave a Reply

Your email address will not be published. Required fields are marked *