13

我想看看这里设置了哪些系统属性(以及设置了哪些值),所以最简单的方法(如果不在这里编写新的 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>
4

2 回答 2

6

当您进行谷歌搜索时,结果指向 ant-nodeps-1.7.1.jar
确保 jar 存在并且您已将其添加到类路径中

对于您问题的第二部分:
解决方案 1.您不需要修改 CLASSPATH 变量。相反,您可以通过添加参数-cp [JAR FILE LOCATION]来添加它(-cp 用于“java”可执行文件)
解决方案 2. Jar 文件只是 zip 文件,打开 ant-nodeps.jar 将其内容复制到 ant.jar throw away 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>
于 2011-03-09T23:21:01.433 回答
2

我下载了Ant 1.7.1并查看了它附带的文档。在那里它被描述echoproperties为一个可选任务,但没有提到从哪里获取这个可选任务的 jarfile。

查看 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 现在是一项标准任务。

于 2011-03-10T00:02:08.747 回答