如何在不检查依赖项是否已下载的情况下获取 ivy:cachepath 位置

how to get ivy:cachepath location without checking if dependencies downloaded

我有一个任务在我的 build.xml:

<target name="init" depends="init-ivy">
  ...
  <ivy:cachepath
      inline="true"
      module="jersey-container-servlet"
      organisation="org.glassfish.jersey.containers"
      pathid="jersey.classpath"
      revision="2.23.2" />
  ...
</target>

此任务会在必要时下载 ivy(init-ivy 实际上会这样做),然后调用 ivy 来下载依赖项。它设置 jersey.classpath 作为结果。

现在我的 build 任务取决于 init 任务。所以每次我构建时,它都会检查是否需要安装依赖项。我想避免每次都检查依赖关系,并将 buildinit 分开。但是 init 设置 jersey.classpath 并且 build 使用它。

有没有一种方法可以从 ivy 获取 jersey.classpath 而不要求它检查依赖关系?在这种情况下不检查依赖项甚至是一个好习惯吗?

如本回答中所述,ivy 不会在每次运行时都下载 jar。它在“~/.ivy2/cache”下本地缓存它们:

其次,您在内联模式下使用 ivy,大概是为了避免在后台创建 ivy file? ivy cachepath is classified as post resolve task, which means it will automatically call the resolve 任务。内联模式所做的是告诉 ivy 每次执行新的解析,如果您要管理多个类路径,这是一种浪费。

你最后考虑过使用ivy文件吗?对 resolve 任务的一次调用可以处理所有项目的依赖项,在本地缓存此信息,然后确定是否需要下载文件。我建议始终解决依赖关系。它的成本不高,并且构建之间可能发生了变化(例如,如果您使用的是动态依赖项或 Maven 快照)。

这是我的常春藤标准 Ant 目标:

  <available classname="org.apache.ivy.Main" property="ivy.installed"/>

  <target name="resolve" depends="install-ivy">
    <ivy:resolve/>

    <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

    <ivy:cachepath pathid="compile.path" conf="compile"/>
    <ivy:cachepath pathid="runtime.path" conf="runtime"/>
    <ivy:cachepath pathid="test.path"    conf="test"/>
  </target>

  <target name="install-ivy" unless="ivy.installed">
    <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.4.0/ivy-2.4.0.jar"/>
    <fail message="Ivy has been installed. Run the build again"/>
  </target>

备注:

  • 名为 "resolve" 的单个目标调用 ivy 来管理我的项目类路径。它还会生成有用的文档报告
  • cachepath task is using ivy configurations 对 ivy 文件中的依赖项进行分组或分类。这真的是在单一分辨率上节省时间的效率魔法。
  • 注意 install-ivy 任务如何进行条件检查以确定是否安装了 ivy。由于复杂性,此技巧对于项目的其余依赖项不可行。我的建议是确保常春藤存在,然后用它来管理其他一切。 (在引擎盖下,它会尽最大努力提高效率)。我真的不明白为什么 Apache Ant 不捆绑 ivy jar。