2013-01-07

JavaFX and Maven


Making Maven play along nicely with the JavaFX (2.x) runtime is not really something that can be achieved quite easily.

Unfortunately, the file "jfxrt.jar" which represents the JavaFX runtime can be found in most recent's JRE's "lib" folder but is not appended to the classpath of the Java Compiler when being invoked without any additional parameters.
On the other hand, the native components (shared libs) that are necessary to execute JavaFX applications are placed in the lib/${arch} folder of your JRE and can be loaded without any further problems.

To build your JavaFX project using Maven you basically have two options:
Install the JavaFX runtime into your local maven repository (.m2). A rather good description can be found on the dzone webpage: http://java.dzone.com/articles/install-javafx-runtime-local
Declare a "system scope" dependency in you pom.xml
At first, option 1 seems to be the better solution, because we all know that system scope dependencies are evil. I thought about that for a while and finally realized, that since jfxrt.jar is already a part of the Java runtime, even though it is not (yet) added to the classpath, I prefer option 2.

I strongly believe that one day jfxrt.jar will be added to the compiler's classpath I think it might be the best solution to add a profile to your pom.xml that enables the evil system scope dependency only if needed.

A typical pom.xml of mine looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupId>com.cathive.fx</groupId>
  <artifactId>javafx-example-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <properties>
    <javafx.runtime.jar>${java.home}/lib/jfxrt.jar</javafx>
  </properties>
  <profiles>
    <profile>
      <id>javafx</id>
      <dependencies>
        <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>javafx-runtime</artifactId>
          <version>RELEASE</version>
          <scope>system</scope>
          <systemPath>${javafx.runtime.jar}</systemPath>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
</project>

To build your project it is usually enough to enable the profile "javafx":
mvn -Pjavafx ...


If your JavaFX runtime is NOT yet present in you JRE's "lib"-folder, you'll have to specify an additional variable ("javafx-runtime-jar"):

mvn -Pjavafx -Djavafx.runtime.jar="C:\Program Files\LocationToJavaFXRuntime\jfxrt.jar" ...

No comments:

Post a Comment