Ant:无法创建任务或键入fx:deploy

时间:2017-11-20 23:42:02

标签: java ant

我正在尝试使用Ant将我的Java应用程序打包为一个独立的应用程序。但是,当我尝试使用ant-javafx.jartaskdef加载JavaFX Ant任务时,我无法创建任务或输入'错误。我的dist目标如下(jar目标及其所有依赖项执行时没有错误):

<target name="dist" depends="jar">
    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
                                 uri="javafx:com.sun.javafx.tools.ant"
                                 classpath="${env.JAVA_HOME}/lib/ant-javafx.jar"/>

    <fx:deploy outdir="${antbuild}/packager" outfile="InventoryManager" nativeBundles="exe">

        <fx:application name="Inventory Manager" mainClass="inventorymanager.RunInventoryManager" version="1.0"/>

        <fx:resources>
            <fx:fileset dir="${dist}" includes="InventoryManager-${DSTAMP}.jar"/>
        </fx:resources>

        <fx:info title="Inventory Manager" description="Inventory manager for a store.">
            <fx:association extension="inv" description="Inventory File"/>
        </fx:info>

        <fx:bundleArgument arg="win.menuGroup" value="Inventory Manager"/>

    </fx:deploy>
</target>

当我运行此目标时,出现以下错误:

BUILD FAILED
C:\Users\John\eclipse-workspace\InventoryManager\src\inventorymanager\build.xml:45: Problem: failed to create task or type javafx:com.sum.javafx.tools.ant:deploy
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet

<echo>${env.JAVA_HOME}/lib/ant-javafx.jar</echo>C:\Program Files\Java\jdk1.8.0_151/lib/ant-javafx.jar,所以我知道JAVA_HOME指的是正确的位置。

路径是否包含前向和反斜杠的混合这一事实会产生问题吗?如果没有,我还能尝试什么?

如果这与问题有关,我在Windows 10上从Eclipse Oxygen内部调用Ant。

更新:我尝试使用找到的解决方案here。这是不成功的,所以我尝试将${env.JAVA_HOME}更改回${java.home},以便他们都指向同一个地方<echo>${java.home}/lib/ant-javafx.jar</echo>C:\Program Files\Java\jdk1.8.0_151\jre/lib/ant-javafx.jar,这不是正确的路径JAR文件。有没有办法到C:\Program Files\Java\jdk1.8.0_151\而不诉诸于使用环境变量?

现在我已经知道如何打开详细信息,dist任务的详细输出如下:

parsing buildfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/lib/ant-javafx.jar!/com/sun/javafx/tools/ant/antlib.xml with URI = jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/lib/ant-javafx.jar!/com/sun/javafx/tools/ant/antlib.xml from a zip file
 [macrodef] creating macro  javafx:com.sun.javafx.tools.ant:init-ant

按要求提供完整示例:

C:\ Users \ John \ eclipse-workspace \ HelloWorld \ src \ helloworld \ HelloWorld.java包含:

package helloworld;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

C:\ Users \ John \ eclipse-workspace \ HelloWorld \ src \ helloworld \ build.xml包含:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World" default="dist" basedir="C:/Users/John/eclipse-workspace/HelloWorld" xmlns:fx="javafx:com.sum.javafx.tools.ant">
    <description>
            Ant build of a self-contained Hello World application.
    </description>

    <!-- Set directory properties for this build -->
    <property name="antbuild" location="antbuild"/>
    <property name="src" location="src"/>
    <property name="dist" location="dist"/>
    <property environment="env"/>

    <target name="init">
        <tstamp/>
        <mkdir dir="${antbuild}"/>
    </target>

    <target name="compile" depends="init" description="Compiles the source code.">
        <javac includeantruntime="false" srcdir="${src}" destdir="${antbuild}"/>
    </target>

    <target name="jar" depends="compile" description="Creates the JAR file for the application.">

        <manifest file="${antbuild}/MANIFEST.MF">
            <attribute name="Main-Class" value="helloworld.HelloWorld"/>
        </manifest>

        <jar manifest="${antbuild}/MANIFEST.MF" jarfile="${dist}/HelloWorld-${DSTAMP}.jar" basedir="${antbuild}"/>
    </target>

    <echo>${env.JAVA_HOME}/lib/ant-javafx.jar</echo>
    <target name="dist" depends="jar">
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
                uri="javafx:com.sun.javafx.tools.ant"
                classpath="${env.JAVA_HOME}/lib/ant-javafx.jar;src"/>

        <fx:deploy outdir="${antbuild}/packager" outfile="HelloWorld" nativeBundles="exe">

            <fx:application name="Hello World" mainClass="helloworld.HelloWorld" version="1.0"/>

            <fx:resources>
                <fx:fileset dir="${dist}" includes="HelloWorld-${DSTAMP}.jar"/>
            </fx:resources>

            <fx:info title="Hello World" description="Hello World program."/>

            <fx:bundleArgument arg="win.menuGroup" value="Hello World"/>

        </fx:deploy>
    </target>

    <target name="cleanup">
        <!-- Deletes the antbuild directory -->
        <delete dir="${antbuild}"/>
        <delete dir="${dist}"/>
    </target>

</project>

尝试运行dist build.xml目标会产生以下(非详细)输出:

Buildfile: C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\build.xml
        [echo] C:\Program Files\Java\jdk1.8.0_151/lib/ant-javafx.jar

init:
       [mkdir] Created dir: C:\Users\John\eclipse-workspace\HelloWorld\antbuild

compile:
       [javac] Compiling 1 source file to C:\Users\John\eclipse-workspace\HelloWorld\antbuild

jar:
         [jar] Building jar: C:\Users\John\eclipse-workspace\HelloWorld\dist\HelloWorld-20171121.jar

dist:

BUILD FAILED
C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\build.xml:36: 
Problem: failed to create task or type javafx:com.sum.javafx.tools.ant:deploy
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet


Total time: 1 second

1 个答案:

答案 0 :(得分:0)

我的第一个问题:问:你ant-javafx.jar中有jdk_home/lib吗?答案似乎是&#34;是&#34;。

问:您的xmlns:fx="javafx:com.sun.javafx.tools.ant"根元素中有<project>吗?

问:Line 45究竟是什么?

另外:请务必阅读Oracle文档:

https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/javafx_ant_tasks.html

附录:

请查看此处的建议(您的确切错误以及一些可能的解决方法):JAVAFx Build Failed

更新: 根据您的更新,听起来您想要为Ant构建设置JRE:

Eclipse&gt;你的项目&gt; build.xml&gt; <外部工具配置> JRE选项卡&gt;      &lt; =选择您想要的JRE

重要的是要注意Windows变量&#34; JAVA_HOME&#34;是 NOT 是否由Eclipse或Ant直接使用。

同样重要的是要注意,用于Ant构建的JRE可能(通常)与您用于项目的JRE不同。