我一直在关注一些示例源代码,但在执行 ftp 任务时出现错误。这是我的 build.xml 文件:
?xml version="1.0"?>
<project name="HelloWorld" default="deploy">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init">
<javac includeantruntime="false"
srcdir="src"
destdir="build/classes"/>
</target>
<target name="doc" depends="init">
<javadoc destdir="build/classes"
sourcepath="src"
packagenames="org.*" />
</target>
<target name="package" depends="compile,doc">
<jar destfile="dist/project.jar"
basedir="build/classes" />
</target>
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
<classpath>
<pathelement location="/usr/local/ant/lib/ant-commons-net.jar"/>
</classpath>
</taskdef>
<target name="deploy" depends="package">
<ftp classname="org.apache.tools.ant.taskdefs.optional.net.FTP"
server="${server.name}"
userid="${ftp.username}"
password="${ftp.password}">
<fileset dir="dist" />
</ftp>
</target>
</project>
以下是一些设置:
$ ant -version
Apache Ant(TM) version 1.9.0 compiled on March 5 2013
$ echo $ANT_HOME
/usr/local/ant
$ ls -l $ANT_HOME
lrwxrwxrwx 1 root root 17 May 20 11:54 /usr/local/ant -> apache-ant-1.9.0/
这是我得到的错误的输出。我认为 ant 构建文件应该是可移植的。这里有什么问题?不知何故,“Ant” FTP 任务会查找来自未随 ant 分发的类的类名。我尝试将任务指向与 ant 一起分发的类,但 ant 似乎仍然没有找到与它正在寻找的 ftp 任务对应的类。
Buildfile: /home/johndoe/ex/build.xml
init:
compile:
doc:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package org.example.helloworld...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.7.0_21
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
package:
[jar] Building jar: /home/johndoe/ex/dist/project.jar
deploy:
BUILD FAILED
/home/johndoe/ex/build.xml:30: Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig
at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.getValidLanguageCodes(FTP.java:2698)
at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.<clinit>(FTP.java:2694)
at org.apache.tools.ant.taskdefs.optional.net.FTP.<init>(FTP.java:137)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.apache.tools.ant.AntTypeDefinition.innerCreateAndSet(AntTypeDefinition.java:328)
at org.apache.tools.ant.AntTypeDefinition.createAndSet(AntTypeDefinition.java:274)
at org.apache.tools.ant.AntTypeDefinition.icreate(AntTypeDefinition.java:219)
at org.apache.tools.ant.AntTypeDefinition.create(AntTypeDefinition.java:206)
at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:285)
at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:263)
at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:417)
at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
at org.apache.tools.ant.Task.perform(Task.java:347)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.net.ftp.FTPClientConfig
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 26 more
Total time: 2 seconds
理想情况下,我不应该下载其他类来让 ant 工作。它应该开箱即用,否则它不是那么便携。另外,如果有人可以帮助我在哪里下载缺少的课程,那将是非常有帮助的。
谢谢。