28

Is it possible to reference JavaFX 2.0 as a dependency in Maven in the pom.xml so that everything works smoothly?

I saw in this question that it is possible to install the jfxrt.jar locally, but ideally I'd like a simpler approach where the dependency can be properly resolved and downloaded without any local hacks....

Community
  • 1
  • 1
mikera
  • 105,238
  • 25
  • 256
  • 415

3 Answers3

19

here is a possible solution.

Create a lib folder in your project directory and put the jfxrt.jar into that directory.

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>javafx</artifactId>
  <version>2.2.3</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/jfxrt.jar</systemPath>
</dependency>

And if you want to build an executable jar you only need to include the javafx-maven-plugin. For further information see: link-to-github

<plugin>
      <groupId>com.zenjava</groupId>
      <artifactId>javafx-maven-plugin</artifactId>
      <version>1.3</version>
      <configuration>
          <mainClass>[put your application main class here]</mainClass>
      </configuration>
</plugin>
Andreas Stotter
  • 191
  • 1
  • 2
11

No, such a solution doesn't exist for the moment. I doubt that it will ever, since as from Java 1.7 update 2, JavaFX is installed together with the JVM. Plans are to include JavaFX in the JRE as from Java 1.8 (next year). From then on, you wouldn't need a dependency at all.

However you can use Maven with JavaFX now already, because Maven can call Ant tasks (antrun plugin) and there are super Ant tasks available with the JavaFX distribution. I blogged about it, but it's in French: Creer un projet Maven pour JavaFX2. It walks you through the whole process. Nevertheless, if you don't understand French, leave a comment on the article and I will try to help you or look in here in the Oracle forum

estiedi
  • 384
  • 2
  • 12
  • Adding to this already good reply, I just like to point to Adam Bien's post: http://www.adam-bien.com/roller/abien/entry/how_to_compile_java_fx – Mathias Conradt Jun 27 '12 at 15:58
  • 1
    JavaFX will be removed from the Java JDK [11] : https://www.infoworld.com/article/3261066/java/javafx-will-be-removed-from-the-java-jdk.html - in the meantime someone could create a future-proof dummy dependency or something similar? – Johannes Apr 01 '18 at 06:12
  • 1
    This answer is no longer useful. It was hardly useful as it is barely more then a link-only answer, but the blog article is no longer available and JavaFX is removed from the JDK, so the first paragraph is untrue as well. – Polygnome Dec 06 '18 at 16:22
  • If someone ends up here via Google like me, this all seems horribly outdated. I've found that there are actually Maven Central artifacts to use, compare https://search.maven.org/search?q=org.openjfx – pete83 Mar 24 '19 at 15:20
7

The article of Sergey suggests to add javafx as a system dependency, which should not be used. Instead, you can include the following in your POM to install javafx automatically.

    <profile>
        <id>install-javafx</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.3.1</version>
                    <executions>
                        <execution>
                            <id>install-javafx</id>
                            <goals>
                                <goal>install-file</goal>
                            </goals>
                            <phase>validate</phase>
                            <configuration>
                                <file>${jfx-runtime}/lib/jfxrt.jar</file>
                                <groupId>javafx</groupId>
                                <artifactId>javafx</artifactId>
                                <version>${jfx-version}</version>
                                <packaging>jar</packaging>
                                <javadoc>${jfx-runtime}/../docs/api.zip</javadoc>
                        <!--<sources>no source available</sources>-->
                            </configuration>
                        </execution>
                    </executions>
                </plugin>                    
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>install-javafx-bin</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${settings.localRepository}/javafx/javafx</outputDirectory>
                                <useBuildFilters>false</useBuildFilters>
                                <resources>
                                    <resource>
                                        <directory>${jfx-runtime}</directory>
                                        <includes>
                                            <include>bin/*.dll</include>   
                                        </includes>
                                    </resource>
                                </resources>              
                            </configuration>            
                        </execution>
                    </executions>
                </plugin>
            </plugins>    
        </build>
    </profile>

If you want to have api docs installed, zip the contents of of the docs/api folder to docs/api.zip. Now you just have to run maven, with the profile activated and the jfx-runtime and jfx-version properties set.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • First time I've seen this solution, and convinced this is the right way to do it. Wish this was the most advocated way. How do you suggest we handle `${jfx-runtime}` and `${jfx-version}`. Just hard code them in the `` section? – metasim Jun 27 '12 at 16:17
  • I have the version hard coded in the properties, because then I can reuse it in the dependencies. The runtime path should not be hard coded, because it depends on the platform. – Cephalopod Jul 03 '12 at 07:36
  • This is the typical "correct" / approved way that avoids system dependency. It is *also* the typical monstrously complicated way (to avoid the system dependency) – WestCoastProjects Aug 11 '15 at 03:47
  • 1
    @Cephalopod what should be the possible value for `jfx-runtime` ? (not hard coded path). – Junaid Dec 02 '16 at 07:25