常见的问题:Ivy无法解析依赖项,找不到原因

34

使用 ivy:retrieve 时,无法解析应该下载的依赖项。 输出如下:

Buildfile: C:\Users\Simon\workspace\apollo\build.xml
init:
resolve:

BUILD FAILED
C:\Users\Simon\workspace\apollo\build.xml:42: Problem: failed to create task or type antlib:org.apache.ivy.ant:retrieve
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

This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -C:\Users\Simon\eclipse\plugins\org.apache.ant_1.8.2.v20120109-1030\lib
        -C:\Users\Simon\.ant\lib
        -a directory added on the command line with the -lib argument


Total time: 348 milliseconds

相关部分的build.xml如下:

  <target name="resolve" depends="init">
    <ivy:retrieve pattern="${lib}/[artifact]-[revision].[ext]" sync="true" />
  </target>

这里还有一个列表,列出了它应该从build.xml中下载的内容

  <target name="doc" depends="build">
    <javadoc sourcepath="${src}" classpathref="libraries" access="private" destdir="${doc}" windowtitle="Apollo">
      <doclet name="org.jboss.apiviz.APIviz" pathref="libraries">
        <param name="-sourceclasspath" value="${bin}" />
        <param name="-author" />
        <param name="-version" />
        <param name="-use" />
        <param name="-nopackagediagram" />
      </doclet>
      <doctitle><![CDATA[<h1>Apollo</h1>]]></doctitle>
      <link href="http://download.oracle.com/javase/6/docs/api/" />
      <link href="http://docs.jboss.org/netty/3.2/api/" />
      <link href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/" />
      <link href="http://www.junit.org/apidocs/" />
      <link href="http://commons.apache.org/compress/apidocs/" />
      <link href="http://jruby.org/apidocs/" />
    </javadoc>
  </target>
5个回答

57

ANT无法找到ivy jar,需要从下载、提取文件,并将ivy-x.y.z.jar放置在以下位置之一:

  • $ANT_HOME/lib
  • $HOME/.ant/lib

启用Ivy

Ivy被打包为一个antlib,所以要启用它,您需要执行以下步骤:

1) 在构建文件的顶部声明ivy命名空间。

<project ..... xmlns:ivy="antlib:org.apache.ivy.ant">

2) 在其中一个ant库目录中包含ivy jar。

您的错误消息指出了一些可能的antlibs位置:

This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -C:\Users\Simon\eclipse\plugins\org.apache.ant_1.8.2.v20120109-1030\lib
        -C:\Users\Simon\.ant\lib
        -a directory added on the command line with the -lib argument

注意:

antlib 的美妙之处在于您不需要执行 taskdef(如果您想将 ivy jar 放置在非标准位置,则此步骤是可选的)

如何引导构建

尽管ivy是ANT的子项目,但出于某种莫名其妙的原因,它未与ANT捆绑在一起....

我通常在我的构建文件中包含以下目标以设置新环境:

<target name="bootstrap" description="Used to install the ivy task jar">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"/>
</target>

它会从Maven中央仓库下载Ivy Jar包。

由于所有其他ANT任务都可以随后使用Ivy下载,因此很少有人对构建文件顶部的这个小丑角感到不满。


快速反馈问题:如果ivy在你的ant_home/lib中,你就不需要taskdef吗?这对我来说是全新的:D - oers
3
是的。令人困惑的是,ANT项目为什么不默认安装ivy jar包。 - Mark O'Connor
2
在运行引导程序后,您可能需要重新启动Eclipse。 - user2066936

11

如果您无法将 ivy 库放入 Ant 的类路径中,则需要自己定义它:

<path id="ivy.lib.path">
    <fileset dir="path/to/dir/with/ivy/jar" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
         uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>

这段内容在入门教程中缺失,但在此处列出:http://ant.apache.org/ivy/history/2.2.0/ant.html


如果我在 Eclipse 中构建项目,这似乎已经包含了所有的 Ant 工具,这会有任何区别吗? - Simon Sheehan
@SimonSheehan ivy并不是ant的一部分,它只是一个额外的库,因此在使用eclipse时并没有任何区别,你必须定义任务(当然,在使用之前)。 - oers
在 build.xml 文件中,我不确定应该把它放在哪里。随便放在任何地方吗? - Simon Sheehan
@SimonSheehan,你的init目标是个好地方,taskef必须在任何解析之前执行。 - oers
1
如果您正在使用 Eclipse,则不需要手动放置 Ivy jars。相反,请使用 Ivy Eclipse 更新网站,安装“Apache Ivy”和“Apache Ivy Ant 任务”。 - approxiblue

5

当您运行 Ant 任务时,请确保 ivy.jar 存在于classpath中。在 Eclipse 中 ->“Run As”- >“Ant Build” ->“Edit Configuration” ->“Classpath”选项卡中。尽管 Eclipse 在 ANT Home 中有 ivy.jar,但由于某种原因它没有被调用。


1
即使将 ivy jar 添加到 ant lib 中,仍然无法被选中。再次在 Preferences->Ant->Runtime 中选择 ANT_HOME ,将刷新 lib dir 并接受您添加的任何库。请保留 HTML 标签。

1
我在MacOSX(10.11.6 El Capitan)上遇到了类似的问题。 我使用Brew软件包管理器安装了ant和Ivy。
另一种方法是使用-lib选项手动定义,例如:
ant clean compile -lib /usr/local/Cellar/ivy/2.4.0/libexec/ivy-2.4.0.jar 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接