我想看看这里设置了哪些系统属性(以及哪些值),所以最简单的方法(如果不在这里编写新的Java程序)将在我的ant构建脚本中添加一些行:
<target name="properties">
<echoproperties/>
</target>
但是运行ant会给出我的错误消息:
/u/ebermann/projektoj/stackoverflow-examples/build.xml:19: Problem: failed to create task or type echoproperties
Cause: the class org.apache.tools.ant.taskdefs.optional.EchoProperties was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-/usr/share/ant/lib
-/u/ebermann/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
好的,所以我不会惊慌,但不知道该怎么做。
我这里有Ant 1.7.1(一个OpenSUSE系统),遗憾的是没有这个版本的文档,而且我不是root用来安装当前的ant版本或旧版本的文档(我刚下载了它它仍然没有说这里需要哪个jar文件)。在上面列出的目录中,只有/usr/share/ant/lib
存在,但它不包含optional
。
我想下载必要的jar文件并将其放在我的主目录中,但在哪里找到它? ant download archive不包含任何内容,我不知道在哪里搜索。 (我确实谷歌了一下,但没有找到任何东西。
那么,有人可以给我一些指示,找到合适的jar文件吗?
(我认为解决方案非常简单,有些事情只是阻碍了我的观点。)
在 vahapt 的回答之后,我从apache repository下载了该文件,并将其放入错误消息中提到的目录/u/ebermann/.ant/lib
中。再次运行ant properties
- 与上面的结果相同。
$ jar -tf /u/ebermann/.ant/lib/ant-nodeps-1.7.1.jar | grep 'EchoProperties.class'
org/apache/tools/ant/taskdefs/optional/EchoProperties.class
这看起来应该有效 - 错误信息是错误的吗?
如果我将它直接放入CLASSPATH,它可以工作:
$ CLASSPATH=/u/ebermann/.ant/lib/ant-nodeps-1.7.1.jar ant properties
Buildfile: build.xml
properties:
[echoproperties] #Ant properties
[echoproperties] #Thu Mar 10 00:46:22 CET 2011
...
[echoproperties] user.name=ebermann
[echoproperties] user.timezone=
BUILD SUCCESSFUL
Total time: 0 seconds
我不想更改我的正常CLASSPATH变量,它应该通过将它放入此目录中,或者我是否理解错误?
任何想法,或者这是一个蚂蚁错误?
(另外,为什么ant文档中没有提到这个文件?)
编辑:
在 vahapt 的答案之后,我的ant build文件如下所示:
<project name="stackoverflow-examples" basedir=".">
<target name="echoproperties.prepare">
<available property="echoproperties.works"
classname="org.apache.tools.ant.taskdefs.optional.EchoProperties"
/>
</target>
<target name="echoproperties.init"
depends="echoproperties.prepare"
unless="echoproperties.works">
<taskdef name="echoproperties" classname="org.apache.tools.ant.taskdefs.optional.EchoProperties">
<classpath>
<fileset dir="${user.home}/.ant/lib">
<include name="ant-nodeps.jar" />
</fileset>
</classpath>
</taskdef>
</target>
<target name="properties" depends="echoproperties.init">
<echoproperties/>
</target>
</project>
只有当任务不在ant类路径中时,才会重新注册该任务。 (因此它也适用于在主目录中没有此文件的完整ant安装。)
我仍然会说This is not a bug; it is a configuration problem
并不完全正确,更重要的是将文件放在指定的目录中 。
另一个有趣的观察结果:${user.home}/.ant/lib
中的nodeps.jar(即现在/u/ebermann/.ant/lib/ant-nodeps.jar
)已经在类路径中(由${java.class.path}
显示的那个,但这似乎没有帮助<echoproperties>
在没有taskdef
的情况下工作。
所以,这也有效:
<target name="echoproperties.init"
depends="echoproperties.prepare"
unless="echoproperties.works">
<taskdef name="echoproperties"
classname="org.apache.tools.ant.taskdefs.optional.EchoProperties"
classpath="${java.class.path}" />
</target>
答案 0 :(得分:6)
进行谷歌搜索时,结果指向ant-nodeps-1.7.1.jar
确保jar存在并且已将其添加到类路径
对于你问题的第二部分:
解决方案1。您无需修改CLASSPATH变量。相反,您可以通过添加参数 -cp [JAR FILE LOCATION] (-cp用于“java”可执行文件)添加它
解决方案2。 Jar文件只是zip文件,打开ant-nodeps.jar将其内容复制到ant.jar扔掉ant-nodeps.jar
解决方案3。请参阅下面的示例。 taskdef是一个将jar或类加载到ClassLoader层次结构中的ant功能。 (在使用之前加载类,就像魅力一样)
<?xml version="1.0" encoding="ASCII"?>
<project name="Test" default="properties" basedir=".">
<target name="properties" depends="init">
<echoproperties/>
</target>
<target name="init">
<taskdef name="echoproperties" classname="org.apache.tools.ant.taskdefs.optional.EchoProperties">
<classpath>
<fileset dir="${ant.library.dir}">
<include name="ant-nodeps.jar" />
</fileset>
</classpath>
</taskdef>
</target>
</project>
答案 1 :(得分:2)
我下载了Ant 1.7.1并查看了随附的文档。在那里,它将echoproperties
描述为可选任务,但未提及在何处获取此可选任务的jar文件。
查看lib文件夹,我发现了ant-nodeps.jar
。显然,它包含在Ant 1.7.1中。
我建议您下载并安装Ant 1.8。由于Ant是一个Java jar文件,因此安装最新最好的版本并不是那么困难。
我查看了我的Mac,/usr/bin/ant
是指向/usr/share/ant/bin
的链接,/usr/share/ant/
是指向/usr/share/java/ant-1.8.2
的链接。所以,我所要做的就是将/usr/share/ant/bin/
指向正确的Ant版本。
在Ant 1.8.2中,echoproperties现在是一项标准任务。