May 11, 2009

Maven and JavaFX, the story of TwitterFX POM

JavaOne demo


Our JavaOne 2009 technical presentation TS-4388 is a demo integrating Maven, JavaFX, Artifactory and WebStart in one smooth process for the developer and site operation manager.
So, we were looking for a nice JavaFX demo with source code using external Java dependencies that will demonstrate the ease of use of Maven when using Java libraries. From the JavaFX Application contest and from some Twitter news we tried TwitterFX.
TwitterFX
We were immediately impressed by the UI quality and slickness of this application: Bravo!
So we checked out the source code, and we saw the need for Maven on this project. All the dependent jars were included in Subversion (3.1M of jars out of a 4.2M project: 74%), and the generated build scripts generated by Netbeans are hard to maintain for users without the IDE. Managing Hudson builds without NetBeans becomes almost impossible.

JavaFX and Maven


We looked for java developers that already tried to merge these 2 technologies, and found out that few actually succeeded. We were looking for a clean integration with minimal POM configuration.
  1. For compiling JavaFX code, we found:
  2. For generating JNLP files, the WebStart plugin is working correctly but it is flattening the references to external jars. It means that all dependent jars are copied in the same folder than the jar of your application. We want to avoid the duplication of jars in the end user WebStart cache, so we liked to use the maven repository layout format for referencing jars.

So, we wrote 2 Maven plugins:

  1. JavaFX Compiler Maven Plugin: compile javafx source files without interfering with the java compiler. It activates the javafxc Ant task using the Maven model to fill the classpath and other parameters.

  2. JNLP Generator Maven Plugin: The plugin will activate Velocity to generate a JNLP file out of the master template and all Maven model data (your project information) and especially maven runtime dependencies. The generated JNLP file will be distributed by Maven automatically.

We used them first on the small demo that depends only on the JavaFX platform, then we used them to "Mavenize" the TwitterFX code base.
So, here is the source code for the Maven version of TwitterFX. In this check out there are no jars in Subversion, no Ant build script, and no classpath. There was 500 lines of Ant code (XML and properties files, managed and generated by NetBeans), that became 200 lines in the POM file (and can be a lot smaller). The main difference is that the POM file is readable and maintainable.
To build it, you'll need java 6, Maven 2.0.9 or above (2.1 recommended) and to run "mvn -Pjfrog install".
We still have one issue today: It is not possible to create a Maven AND JavaFX project inside NetBeans (NetBeans: Maven + JavaFX project? - The Java Posse | Google Groups).

Deploying the Application


In a modified version of Artifactory we are managing WebStart requests toward JNLP and jar files. This extension provides codebase modification of JNLP file attribute based on the server settings, and on-the-fly signing of the main jar and its dependencies. If you deployed a JNLP applet you can even preview it directly from Artifactory!

Using the Maven 2 repository layout to reference third party jars allows the end user WebStart cache to reuse the dependent jars URL. To avoid users re-downloading common jars between javafx examples, the javafx.com demo files are all uploaded at the root of this server!

I'm Speaking At JavaOne

5 comments:

  1. WWWWOOOWWW !

    I've been looking for JavaFX plugin for some time now, using the old Plexus solutions I sure needed to split my projects into sub-modules.
    Though I would expect a standard Maven plugin that would execute JavaFX compilation by itself (instead of delegating via ant-task), the bottom line is simplified project structure.

    Thanks froggers!

    I also like the dynamic approach, that when no sensible solution is available those folks at jfrog decided to create on of their own (sounds trivial, but it's no always as simple).

    ReplyDelete
  2. Can you shed a little light on the changes needed to Artifactory to allow for deployment of webstart applications?

    That ability is a real killer feature and would really be a nice way to reduce duplication on the WS cache.

    Thanks

    ReplyDelete
  3. Very nice work! I like the format of your article as well - excellent technical writing.

    ReplyDelete
  4. FYI: We just released a final version of the plugin (jfrog-javafx-plugin:1.0).
    The Artifactory version is 2.1, and is available today in our cloud base solution. A cloud solution can be very interesting since it removes the burden on WebStart distribution to the world :).
    The version 2.1 of Artifactory will be released soon, and the WebStart capability will be a professional addon.

    ReplyDelete
  5. Hi,

    I'm using your JavaFX Compiler Maven Plugin and I enjoy using it.

    It's working nice.

    But I don't understand where can I found JavaFX dependencies if I don't want to install it by hand in my own computer?

    ReplyDelete