5

I have question. How to make fat jar for JavaFX app using Ant? I know that in Internet is a few tutrials, but it concerns Java apps, not JavaFX apps. I conlude that is difference, because i tried to follow this tutorials, and it works for Java apps, but in JavaFX apps with fxml files after attempt run app, it returns NullPointerException: location is required (and yes, i tried modificate path to fxml files, and use ClassLoader, it dosesn't help). I tried use One Jar plugin for Ant too, with the same effect.

I have following build.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="App" default="do-deploy" basedir="."  xmlns:fx="javafx:com.sun.javafx.tools.ant">
<target name="init-fx-tasks">
    <path id="fxant">
        <filelist>
            <file name="${java.home}\..\lib\ant-javafx.jar"/>
            <file name="${java.home}\lib\jfxrt.jar"/>
        </filelist>
    </path>

    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
        uri="javafx:com.sun.javafx.tools.ant"
        classpathref="fxant"/>
</target>
<target name="setup-staging-area">
    <delete dir="externalLibs" />
    <delete dir="project" />
    <delete dir="projectRefs" />

    <mkdir dir="externalLibs" />

    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="commons-cli-1.2.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpcore-ab-4.4.4.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpclient-cache-4.5.1.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpcore-4.4.4.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="jsoup-1.8.3.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpcore-nio-4.4.4.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpcore-4.4.3.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpmime-4.5.1.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="commons-codec-1.9.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="httpclient-4.5.1.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="commons-logging-1.2.jar"/>
        </fileset>
    </copy>
    <copy todir="externalLibs">
        <fileset dir="C:\Users\User\OneDrive\Libraries">
            <filename name="fluent-hc-4.5.1.jar"/>
        </fileset>
    </copy>

    <mkdir dir="project" />
    <copy todir="project">
        <fileset dir="C:\Users\User\OneDrive\Code\java\workspace\App">
            <include name="src/**" />
        </fileset>
    </copy>

    <mkdir dir="projectRefs" />
</target>
<target name='do-compile'>
    <delete dir="build" />
    <mkdir dir="build/src" />
    <mkdir dir="build/libs" />
    <mkdir dir="build/classes" />

    <!-- Copy project-libs references -->
    <copy todir="build/libs">
        <fileset dir="externalLibs">
            <include name="httpclient-cache-4.5.1.jar"/>
            <include name="httpcore-ab-4.4.4.jar"/>
            <include name="jsoup-1.8.3.jar"/>
            <include name="commons-cli-1.2.jar"/>
            <include name="commons-codec-1.9.jar"/>
            <include name="httpcore-4.4.3.jar"/>
            <include name="fluent-hc-4.5.1.jar"/>
            <include name="httpcore-nio-4.4.4.jar"/>
            <include name="httpcore-4.4.4.jar"/>
            <include name="commons-logging-1.2.jar"/>
            <include name="httpclient-4.5.1.jar"/>
            <include name="httpmime-4.5.1.jar"/>
        </fileset>
    </copy>

    <!-- Copy project references -->

    <!-- Copy project sources itself -->
    <copy todir="build/src">
        <fileset dir="project/src">
            <include name="**/*"/>
        </fileset>
    </copy>

    <javac includeantruntime="false" source="1.8" target="1.8" srcdir="build/src" destdir="build/classes" encoding="Cp1250">
        <classpath>
            <fileset dir="build/libs">
                <include name="*"/>
            </fileset>
        </classpath>
    </javac>

    <!-- Copy over none Java-Files -->
    <copy todir="build/classes">
    <fileset dir="project/src">
        <exclude name="**/*.java"/>
    </fileset>
    </copy>


</target>
<target name="do-deploy" depends="setup-staging-area, do-compile, init-fx-tasks">
    <delete file="dist"/>
    <delete file="deploy" />

    <mkdir dir="dist" />
    <mkdir dir="dist/libs" />

    <copy todir="dist/libs">
        <fileset dir="externalLibs">
            <include name="*" />
        </fileset>
    </copy>


    <fx:resources id="appRes">
        <fx:fileset dir="dist" includes="App.jar"/>
        <fx:fileset dir="dist" includes="libs/*"/>
    </fx:resources>

    <fx:application id="fxApplication"
        name="Youtufy"
        mainClass="pl.javastart.App.main.Main"
        toolkit="fx"
    />

    <mkdir dir="build/classes/META-INF" />



    <fx:jar destfile="dist/App.jar">
        <fx:application refid="fxApplication"/>
        <fileset dir="build/classes">
        </fileset>
        <fx:resources refid="appRes"/>

        <manifest>
            <attribute name="Implementation-Vendor" value="Javastart"/>
            <attribute name="Implementation-Title" value="App"/>
            <attribute name="Implementation-Version" value="1.0"/>
            <attribute name="JavaFX-Feature-Proxy" value="None"/>
        </manifest>
    </fx:jar>


    <mkdir dir="deploy" />
    <!-- Need to use ${basedir} because somehow the ant task is calculating the directory differently -->
    <fx:deploy
        embedJNLP="false"
        extension="false"
        includeDT="false"
        offlineAllowed="true"
        outdir="${basedir}/deploy"
        outfile="Youtufy" nativeBundles="none"
        updatemode="background" >

        <fx:platform basedir="${java.home}"/>
        <fx:info title="App" vendor="Javastart"/>

        <fx:application refId="fxApplication"/>
        <fx:resources refid="appRes"/>
    </fx:deploy>


</target>

How to modificate it, to make fat jar with all dependencies in it? I know that its easier to make in Maven, but i want to know, is that possible in Ant. Regards

Stephan
  • 41,764
  • 65
  • 238
  • 329
MikolajMGT
  • 113
  • 2
  • 10
  • I've followed [this][1] post which helped me create a runtime (exe) with all dependencies (I'm not a fan of fat jar's in general - and thats what you specifically asked, others might still wish to get this info.). [1]: http://java-buddy.blogspot.com/2013/12/deploying-javafx-applications-in-self.html – namokarm Feb 24 '19 at 17:41

0 Answers0