How to create Apache Maven project with JavaFX 8 and FXML support

JDK 8 comes with better JavaFX support. Creation of maven project which supports JavaFX is really easy now.

javafx_logo

[wp_ad_camp_1]

Prerequisites

Create maven project

At first create a new maven Java Application project in your favorite IDE.


Modify pom.xml

Insert section below before element <project> in your pom.xml and modify <finalName> and <mainClass> by your project.

The most important maven plugin in a pom.xml is “JavaFX Plugin”, you can do with them amazing things such as creates MSI, DEB and RPM packages and much more. Please see documentation for the plugin

<build>
    <finalName>MyJavaFX</finalName>
    <plugins>
       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
             <source>1.8</source>
             <target>1.8</target>
          </configuration>
        </plugin>        
        <plugin>
           <groupId>com.zenjava</groupId>
           <artifactId>javafx-maven-plugin</artifactId>
           <version>2.0</version>
           <configuration>
              <mainClass>org.korecky.myjavafx.App</mainClass>
           </configuration>
        </plugin>
    </plugins>    
</build>

[wp_ad_camp_1]

Create project structure

In JavaFX project are java classes stored in src/main/java and FXML, CSS, images, etc. in src/main/resources. There is example of these structure (base package is: org.korecky.myjavafx


│   pom.xml
└───src
    ├───main
        ├───java
        │   └───org
        │       └───korecky
        │           └───myjavafx
        │               │   App.java
        │               └───fxml
        └───resources
            └───org
                └───korecky
                    └───myjavafx
                        ├───css
                        ├───fxml
                        └───images

Done

You are now done.
You can write your code now.

[wp_ad_camp_1]

Resources

Resources which helped me wrote this guide:
From Zero To JavaFX In 5 Minutes
JFXMavenPkgExample
Thanks Carl J. Mosca for help with classpath problem.

Leave a Reply

Your email address will not be published. Required fields are marked *