I decided to have a bit of a play with JavaFX 2.0 the other day as we have a project coming up that may be a good candidate for using it. My first impression of JavaFX 2.0 is generally very positive, it’s a lot like Swing with the sharp edges knocked off. I’d like to see more emphasis on regular controls rather than animations and 3D (as that’s what I want) but we can’t have everything. The big disappointment though is the total lack of support for Maven.
JavaFX 2.0 is making a push to be the way to write rich internet applications (RIAs) it’s logical to assume therefore that you are, at least initially, going to be targeting developers that currently write internet applications – all of those developers build with Maven. Ok, there are a few sadists out there that use Ant or even roll their own but they are few and far between. Only supporting Ant for JavaFX 2.0 really shows that the design and development team comprise mostly desktop developers – this isn’t necessarily a bad thing but couldn’t they have squeezed in one or two web application developers as well?
Anyway, enough with the complaining. This guide will hopefully show you how to get a JavaFX 2.0 application built using Maven. There are numerous articles which claim to show you how to do this (see the references) but I found they all had serious problems which stopped them from working.
Create a Maven Project
I’m going to be building my JavaFX 2.0 application in NetBeans 7.1. For the moment I’m going to concentrate on creating a runnable jar file, later I’ll look at making a JNLP and applet deployable as well. I’m going to assume that you have JavaFX 2.0 already installed along with Java 7 update 2 or later. I’m also going to assume that you have at least a workable understanding of Maven – for example you know roughly what the POM and settings files are for.
In NetBeans create a standard Maven application project then place a simple little “Hello World” JavaFX 2.0 application under source – if you create a regular Ant based JavaFX project in NetBeans it comes with a “Hello World” app so you could use this.
Provding the JavaFX 2.0 Dependency
There’s been many howls of complaint about the fact that the JavaFX 2.0 runtime can’t be included in the main Maven repository so I won’t go into that in any more detail here. At the end of the day you need to provide the runtime to your application so that it can be built and there are three ways to do this.
Method One – Tweek Your Maven Cache
If you are a lone developer you can place the jfxrt.jar in your local Maven cache. If you aren’t a lone developer this solution is nasty as you’ll be depending on everyone to update their cache at the same time. I’m not going to provide details of this because it’s not a good solution, they should be easy enough to find using Google.
Method Two – Run Nexus
Run a Maven repository such as Nexus and place the runtime jar in it. This isn’t a bad solution but it’s a lot of work if this is the only reason you are running Nexus. You have to keep everything up to date manually as well which sucks.
Method Three – Use a System Dependency
My preferred option is to use a system dependency. This also isn’t ideal because it relies on all the developers keeping their settings file up to date but to me it feels like the least worst option (and I run a Nexus repository).
To create the system dependency add this to your pom:
<dependency> <groupId>com.oracle</groupId> <artifactId>javafx</artifactId> <version>2.0</version> <scope>system</scope> <systemPath>${fx.home}</systemPath> </dependency>
then add this to your settings.xml file:
<profile> <id>javafx</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <fx.home>[PATH]/rt/lib/jfxrt.jar</fx.home> <ant.javafx.jar>[PATH]/tools/ant-javafx.jar</ant.javafx.jar> </properties> </profile>
Replace [PATH] with the location of your JavaFX 2.0 install. The ant tools are required later.
This should leave you at the point where you can build your “Hello World” application but you’ll find that it won’t run as a JavaFX applcation. The next article will cover how to get the application running.
References
- http://notaboekje.blogspot.co.uk/2012/02/creer-un-projet-maven-pour-javafx.html – close but no cigar, the ant tasks won’t run.
- http://www.adam-bien.com/roller/abien/entry/how_to_compile_java_fx – the simplest way to satisfy the JavaFX dependency.
- http://www.zenjava.com/2012/02/17/javafx-and-maven/ – well written but stops short of doing anything useful.
- http://blog.webeo.fr/post/2012/01/29/How-to-build-and-package-JavaFX-2-Application-with-Maven – nice try but doesn’t create a runnable application.