我想看看这里设置了哪些系统属性(以及设置了哪些值),所以最简单的方法(如果不在这里编写新的 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 系统),遗憾的是没有此版本的文档,而且我不是安装当前 ant 版本或旧版本文档的 root 用户(我刚刚下载了它,它仍然可以这里不说需要哪个jar文件)。在上面列出的目录中,仅/usr/share/ant/lib
存在,但它不包含类似optional
.
我想下载必要的 jar 文件并将其放在我的主目录中,但是在哪里可以找到它呢?ant 下载档案不包含类似的内容,我不知道在哪里可以搜索。(我谷歌了一下,但没有找到任何东西。
那么,有人可以给我一些指示在哪里可以找到正确的 jar 文件吗?
(我想解决方案很简单,只是有些东西挡住了我的视线。)
在vahapt的回答之后,我从apache 存储库下载了文件,并将其放入/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 构建文件如下所示:
<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
一个更有趣的观察:(即 now /u/ebermann/.ant/lib/ant-nodeps.jar
)中的 nodeps.jar已经在类路径中(由 显示的路径${java.class.path}
,但如果没有 this ,这似乎无助于<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>