Deploying JavaFX Applications


12 JavaFX Ant Tasks

This chapter shows how to use Ant to package JavaFX application.

JavaFX Ant tasks and the JavaFX Packager tool are currently the only supported ways to package JavaFX applications. This includes supported versions of the NetBeans IDE, which build JavaFX applications with JavaFX Ant tasks.

This page contains the following topics:

See also the following two Ant Task Reference sections:

12.1 Requirements to Run JavaFX Ant Tasks

The ant-javafx.jar file is required to use these tasks. It is located in the following locations:

  • In JDK 7 Update 6 or later, it is located in jdk_home/lib

  • In a standalone JavaFX installation, it is located in javafx-sdk-home/lib

12.2 JavaFX Ant Elements

There are two categories of Ant elements for JavaFX. Each of the following elements is described in JavaFX Ant Task Reference.

JavaFX Ant Tasks

These elements accomplish the following tasks:

  • Creating double-clickable JAR files

  • Creating an HTML page and deployment descriptor for Web Start applications or applications embedded in a web page

  • Digitally signing an application, when necessary

  • Converting CSS files to binary format

  • Assembling self-contained application packages

See JavaFX Ant Task Reference. For general information about packaging for JavaFX applications, see Chapter 5, "Packaging Basics" and Chapter 6, "Self-Contained Application Packaging."

Ant Helper Parameters

These elements are used by the JavaFX tasks. They are listed and described in JavaFX Ant Helper Parameter Reference.

12.3 Using JavaFX Ant Tasks

To use the JavaFX Ant tasks in the your Ant script, you must load their definitions. An example is shown in the build.xml file in Example 12-1:

Example 12-1 Load JavaFX Ant Task Definitions

<project name="JavaFXSample" default="default" basedir="."
    <target name="default">
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      

Notes about Example 12-1:

  • Ensure that you declare the fx: namespace, shown in bold in Example 12-1, because short names for some of JavaFX tasks are the same as those used for some system tasks.

  • The current directory (".") is added to the classpath to simplify customization using drop-in resources. See Section 6.3.3, "Customization Using Drop-In Resources."

Once JavaFX Ant task definitions are loaded, the javafx.ant.version property can be used to check the version of Ant tasks APIs. Use the following list for version numbers:

  • Version 1.0: shipped in the JavaFX 2.0 SDK

  • Version 1.1: shipped in the JavaFX 2.1 SDK

  • Version 1.2: shipped in the JavaFX 2.2 SDK and JDK 7 Update 6

12.4 Ant Script Examples

Example 12-2 shows an Ant script that uses the <fx:jar> task to build the JAR file and the <fx:deploy> task to build the JNLP and HTML files for web deployment. Other elements, such as <fx:application> and <fx:resources> are types that are described in the <fx:application> and <fx:resources> in the Ant task reference.

Example 12-2 Typical JavaFX Ant Script

<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
<fx:application id="sampleApp"
        name="Some sample app"
        <!-- This application has a preloader class -->
<fx:resources id="appRes">
    <fx:fileset dir="dist" requiredFor="preloader"
    <fx:fileset dir="dist" includes="myapp.jar"/>
<fx:jar destfile="dist/myapp.jar">
    <!-- Define what to launch -->
    <fx:application refid="sampleApp"/>
    <!-- Define what classpath to use -->
    <fx:resources refid="appRes"/>
        <attribute name="Implementation-Vendor"
        <attribute name="Implementation-Title"
        <attribute name="Implementation-Version" value="1.0"/>
    <!-- Define what files to include -->
    <fileset dir="${build.classes.dir}"/>
<fx:signjar keyStore="${basedir}/sample.jks" destdir="dist"
        alias="javafx" storePass="****" keyPass="****">
    <fileset dir='dist/*.jar'/>
<fx:deploy width="${applet.width}" height="${applet.height}"
        outdir="${basedir}/${dist.dir}" embedJNLP="true"

    <fx:application refId="sampleApp"/>
    <fx:resources refid="appRes"/>            
    <fx:info title="Sample app: ${application.title}"
    <!-- Request elevated permissions -->
    <fx:permissions elevated="true"/>