[英]Ant: failed to create task or type fx:deploy
我正在嘗試使用Ant將Java應用程序打包為獨立的應用程序。 但是,當我嘗試使用taskdef
從ant-javafx.jar
加載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。
更新:我嘗試使用此處找到的解決方案。 這沒有成功,因此我嘗試將${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>
嘗試運行build.xml
的dist
目標將產生以下(非詳細)輸出:
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
我的第一個問題:問: jdk_home/lib
是否有ant-javafx.jar
? 答案似乎是“是”。
問: <project>
根元素中是否有xmlns:fx="javafx:com.sun.javafx.tools.ant"
?
問: Line 45
到底是什么?
另外:請確保閱讀Oracle文檔:
https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/javafx_ant_tasks.html
附錄:
請在此處查看建議(您的確切錯誤以及幾種可能的解決方法): JAVAFx生成失敗
更新:根據您的更新,聽起來您想為您的Ant構建設置JRE:
Eclipse>您的項目> build.xml>外部工具配置> JRE選項卡> <=選擇所需的JRE
重要的是要注意,Eclipse或Ant 都不直接使用Windows變量“ JAVA_HOME”。
還需要注意的是,您用於Ant構建的JRE可能(並且經常)不同於您用於項目的JRE。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.