JavaFX 2.0 and Maven – Part 2

The previous part of this article covered how to set up a Maven project for JavaFX 2.0 and how to provide the required dependency. This part of the article will show you how to build a runnable jar file.

To cut a long story short the only really good way (at the moment) to build a JavaFX 2.0 application using Maven is to use the ant tasks that are supplied with the JavaFX 2.0 runtime. This is not an ideal solution but until such time as someone provides native Maven support it’ll have to do.

To do this you need to use the Maven antrun plugin, documentation for which can be found here. Before we do that though we’ll set up a dependency copy operation so add this to your pom:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.3</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>package</phase>
			<configuration>
				<overwritereleases>false</overwritereleases>
				<overwritesnapshots>false</overwritesnapshots>
				<overwriteifnewer>true</overwriteifnewer>
			</configuration>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
		</execution>
	</executions>
</plugin>

An empty Maven antrun plugin looks like this:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.6</version>
	<executions>
		<execution>
			<id>JavaFX Package</id>
			<phase>package</phase>
			<configuration>
				<target>
					
				</target>
			</configuration>
			<goals>
				<goal>run</goal>
			</goals>
		</execution>
	</executions>
	<dependencies>
		<dependency>
			<groupId>com.sun.javafx</groupId>
			<artifactId>javafx-ant</artifactId>
			<version>2.0.beta</version>
			<scope>system</scope>
			<systemPath>${ant.javafx.jar}</systemPath>
		</dependency>
	</dependencies>
</plugin>

If you run this now nothing interesting will happen since the target, which runs the Ant tasks, is empty. The rest of this article will be explaining how to complete the target element so that your JavaFX application builds correctly. Notice that the Ant tasks supplied by JavaFX 2.0 have been added as a depndency of the plugin.

The two most interesting pages of information for this section are JavaFX Ant Tasks and JavaFX Ant Tasks Reference. To quickly get going though here is all you need to supply as a target to build a stand alone application (as long as it doesn’t have dependencies):

<target>
	<taskdef name="jfxjar" classname="com.sun.javafx.tools.ant.FXJar" classpathref="maven.plugin.classpath"/>
	<jfxjar destfile="${project.build.directory}/dist/${project.build.finalName}">
		<fileset dir="${project.build.directory}/classes"/>
		<application name="${project.name}" mainClass="[MAIN-CLASS]"/>
		<resources>
			<fileset dir="${project.build.directory}" includes="*.jar"/>
			<fileset dir="${project.build.directory}/dependency" includes="*.jar"/>
		</resources>
	</jfxjar>
</target>

This will produce an additional folder called dist under your regular target folder when you perform a build. The dist folder will contain a single jar file which, when double clicked, will run your application. The FXJar task creates a few boot strapping files and some additional manifest entries in order to support directly lauching your application. Obviously you need to replace the [MAIN-CLASS] entry with the main class for your application.

The next part of this article will refine the build process and bring in dependencies.