This (hopefully) final instalment of this series of articles on building JavaFX applications with Maven will refine what was done in the previous article. The previous article got us to the point where we were able to build a stand alone JavaFX application this article aims to do that with flare and produce roughly the same output as a regular NetBeans Ant JavaFX project.
The project that I am working on has a dependency on an internal company utilities jar which in turn depends on a bewildering array of other jars. The next step, therefore, is to ensure that this dependency tree is correctly handled by the Maven / Ant hybrid build process. The dependency in the pom looks likes this:
<dependency> <groupId>${project.groupId}</groupId> <artifactId>utilities</artifactId> <version>1.2</version> </dependency>
First thing to do is change the output directory for the dependency copy command. The default is a folder called dependencies under the target directory but it would be a lot more convenient if that folder was called lib and under the dist directory we are creating (as is done by NetBeans). So change the dependency copy configuration to:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <configuration> <outputDirectory>${project.build.directory}/dist/lib/</outputDirectory> <overwritereleases>false</overwritereleases> <overwritesnapshots>false</overwritesnapshots> <overwriteifnewer>true</overwriteifnewer> <excludeScope>system</excludeScope> </configuration> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> </plugin>
Notice that I have also excluded the system scope from the copy since there is no point in including the JavaFX runtime in the distribution – it couldn’t run using just this jar file as JavaFX requires significant numbers of native libraries as well.
Now we want to quickly tidy up the fxjar task in the target of the Ant runner plugin. This ensures that the libraries we are using are correctly referenced in the manifest and we include the fallback class (for what it’s worth).
<jfxjar destfile="${project.build.directory}/dist/${project.build.finalName}"> <fileset dir="${project.build.directory}/classes"/> <application name="${project.name}" mainClass="[MAIN-CLASS]"/> <resources> <fileset dir="${project.build.directory}/dist/" includes="lib/*.jar"/> </resources> <manifest> <attribute name="JavaFX-Fallback-Class" value="com.javafx.main.NoJavaFXFallback"/> </manifest> </jfxjar>
You should now have a fully working Maven build of a stand alone JavaFX application.
The next step is to get NetBeans to run the application using Maven, Just hitting the big green button on fun produces a stack trace claiming it can’t find the class: javafx.application.Application.
Getting netbeans to run the application turned out to be a lot more difficult than I expected so I’ve put the information on it’s own page.