我试图用Ant为我的JavaFX应用程序生成一个可执行jar,而我的jar与JavaFX Packager生成的jar之间的区别是后者包括来自com.javafx.main包的类。
我该如何在我的Ant脚本中告诉将这些类也包含在jar中?

最佳答案

您正在使用的ant文件必须具有特殊的fx-task才能部署jar,而不是ant内置的jar任务。这是使用JavaFX生成jar的示例 Ant 目标:

<target name="jar" depends="compile">
        <echo>Creating the main jar file</echo>
        <mkdir dir="${distro.dir}" />
        <fx:jar destfile="${distro.dir}/main.jar" verbose="true">
            <fx:platform javafx="2.1+" j2se="7.0"/>
            <fx:application mainClass="${main.class}"/>

            <!-- What to include into result jar file?
                 Everything in the build tree-->
            <fileset dir="${classes.dir}"/>

            <!-- Define what auxilary resources are needed
                  These files will go into the manifest file,
                  where the classpath is defined -->
             <fx:resources>
                <fx:fileset dir="${distro.dir}" includes="main.jar"/>
                <fx:fileset dir="." includes="${lib.dir}/**" type="jar"/>
                <fx:fileset dir="." includes="."/>
            </fx:resources>

            <!-- Make some updates to the Manifest file -->
            <manifest>
               <attribute name="Implementation-Vendor" value="${app.vendor}"/>
               <attribute name="Implementation-Title" value="${app.name}"/>
               <attribute name="Implementation-Version" value="1.0"/>
            </manifest>
        </fx:jar>
    </target>

请注意,您必须在脚本中的某处定义taskdef:
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
            uri="javafx:com.sun.javafx.tools.ant"
            classpath="${javafx.sdk.path}/lib/ant-javafx.jar"/>

并且项目标签必须具有fx xmlns参考:
<project name = "MyProject" default ="compile"  xmlns:fx="javafx:com.sun.javafx.tools.ant">

现在,生成的jar文件应包含javafx.main中的类,清单将包含它们作为应用程序的入口。更多信息:
http://docs.oracle.com/javafx/2/deployment/packaging.htm

07-28 02:59
查看更多