Tag Archives: ant

Basic Build Management in Netbeans

The idea of multiple build management is to be able to have several entry points in a project (i.e. Main classes) and be able to build one or the other, with its own parameters, in one click.

Configuration setup

Creating different configurations is the easy part:

  1. Right click on the project in the Projects View
  2. Set Configuration / Customize
  3. In the popup window, click on New and define the Main class associated with that configuration

Let’s assume we now have 2 configurations called Drawing and Printing.
We want to change the build to make sure that the jar file has the right name and to create a version.info file in the distribution folder that gives information about the version. Drawing is on version 1.0 and Printing on version 1.5.

Build Setup

Property Files

We start by adding a property to the config files, found in the nbproject/configs folder, accessible from the Files View in Netbeans:
Drawing.properties file:

jar.name=Drawing.jar
version.num=1.0

Printing.properties file:

jar.name=Printing.jar
version.num=1.5

Common build tasks

Then we add some ant tasks in the -post-jar target in the build.xml file, accessible from the Files View too:

    <target name="-post-jar">
        <!-- Creates a Version info file -->
        <echo file="${dist.dir}/version.info" message="Version number: ${version.num}" />
        <!-- Renames the jar file -->
        <move file="${dist.jar}" tofile="${dist.dir}/${jar.name}" />       
    </target>    

Specific tasks

For the Drawing project, we also want to delete the README.TXT file that Netbeans automatically generates.
We create a new build file called build-drawing.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>

<project name="Project" default="default" basedir=".">
    <import file="build.xml" as="common"/>
    <!-- Depends forces the inclusion of what is in the common build file, which would be overriden otherwise -->
    <target name="-post-jar" depends="common.-post-jar">
         <delete file="${dist.dir}/README.TXT"/>
    </target>
</project>

Finally, we let Netbeans know that it has to use this new build file by adding a line to the Drawing.property file:

buildfile=build-drawing.xml

Deployment

Finally, we want to automatically deploy the distribution to a network drive by copying the whole directory.
We add a deploy target in the build script by adding the following lines to build.xml:

    <!-- 
                ======================
                    DEPLOY SECTION 
                ======================
            -->

    <!-- Deploy to server if possible -->
    <target name="check-deploy">
        <condition property="deploy-is-true">
            <and>
                <available file="${dist.dir}/${jar.name}"/>
                <not> <equals arg1="${deploy.dir}" arg2=""/> </not>
                <available file="${deploy.dir}" type="dir"/>
            </and>
        </condition>
    </target>
    <target name="do-deploy" depends="check-deploy" if="deploy-is-true">
        <echo level="info">Deploying application</echo>
        <copy todir="${deploy.dir}">
            <fileset dir="${dist.dir}"/>
        </copy>
    </target>
    <target name="dont-deploy" depends="check-deploy" unless="deploy-is-true">
        <echo level="info">Application cannot be deployed</echo>
    </target>

    <target depends="clean,jar,check-deploy,do-deploy,dont-deploy" description="Deploy project." name="deploy"/>

We also add a property to the property files:

deploy.dir=S:/Deployment Folder/

You now only need to right click in the ant script and select the newly created “deploy” target to execute that script, which cleans, builds and deploys the project.

Bonus: Mercurial Changeset ID

Finally, we want to include the Mercurial changeset ID to the Version.info file. Let’s also include the build time. This is the full script contained in the common build file:

<?xml version="1.0" encoding="UTF-8"?>

<project name="Common Build" default="default" basedir=".">
    
    <import file="nbproject/build-impl.xml"/>

    <target name="-post-init" description="Get the changeset ID of the current build">
        <exec outputproperty="hg.changeset" executable="hg">
            <arg value="id" />
            <redirector>
                <outputfilterchain>
                    <tokenfilter>
                        <replaceregex pattern=" tip" replace="" flags="g"/>
                    </tokenfilter>
                </outputfilterchain>
            </redirector>
        </exec>
        <tstamp>
            <format property="NOW" pattern="yyyy-MM-dd HH:mm:ss z" />
        </tstamp>
    </target>

    <target name="-post-jar">
        <!-- Creates a Version info file -->
        <echo file="${dist.dir}/version.info" message="Version number: ${version.num}${line.separator}Mercurial Changeset ID: ${hg.changeset}${line.separator}Build time: ${NOW}${line.separator}" />
        <delete file="${dist.dir}/README.TXT"/>
        <move file="${dist.jar}" tofile="${dist.dir}/${jar.name}" />       
    </target>     

    <!-- 
                ======================
                    DEPLOY SECTION 
                ======================
            -->

    <!-- Deploy to server if possible -->
    <target name="check-deploy">
        <condition property="deploy-is-true">
            <and>
                <available file="${dist.dir}/${jar.name}"/>
                <not> <equals arg1="${deploy.dir}" arg2=""/> </not>
                <available file="${deploy.dir}" type="dir"/>
            </and>
        </condition>
    </target>
    <target name="do-deploy" depends="check-deploy" if="deploy-is-true">
        <echo level="info">Deploying application</echo>
        <copy todir="${deploy.dir}">
            <fileset dir="${dist.dir}"/>
        </copy>
    </target>
    <target name="dont-deploy" depends="check-deploy" unless="deploy-is-true">
        <echo level="info">Application cannot be deployed</echo>
    </target>

    <target depends="clean,jar,check-deploy,do-deploy,dont-deploy" description="Deploy project." name="deploy"/>

</project>
Tagged , ,