Ant 在构建时下载一个 Jar

Ant Download a Jar at Build

我正在尝试将 forbiddenapis 检查集成到我的项目中。我已经定义了:

<target name="forbidden-checks" depends="clean, runtime, test">
  <ivy:cachepath organisation="de.thetaphi" module="forbiddenapis" revision="2.2" inline="true" pathid="classpath"/>
  <taskdef uri="antlib:de.thetaphi.forbiddenapis" classpathref="classpath"/>
  <forbiddenapis classpathref="all-lib-classpath" dir="${build.dir}" targetVersion="${javac.version}">
    <bundledsignatures name="jdk-unsafe"/>
    <bundledsignatures name="jdk-deprecated"/>
    <bundledsignatures name="jdk-non-portable"/>
  </forbiddenapis>
</target>

all-lib-classpath 包括所有要被 forbiddenapis 插件检查的文件。我认为 forbiddenapis jar 会进入 ${build.dir}。但是我得到了那个错误:

Problem: failed to create task or type forbiddenapis
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.

您需要为来自 Ivy 的 forbiddenapis 任务声明命名空间:

<project xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:fa="antlib:de.thetaphi.forbiddenapis">
...
    <fa:forbiddenapis ... >

或显式声明任务名称:

<taskdef name="forbiddenapis"
         classname="de.thetaphi.forbiddenapis.ant.AntTask"
         classpath="path/to/forbiddenapis.jar"/>

还是看看文档吧https://github.com/policeman-tools/forbidden-apis/wiki/AntUsage

文件没有下载到您的工作区。 cachpath 任务将做两件事,将 jar 下载并缓存到默认目录“~/.ivy2/cache”,然后基于这些缓存的 jar 创建 Ant 路径。

其次,正如@Denis Kurochkin 所指出的,您正在使用的任务显然需要声明一个名称空间,这在现代 Ant 任务中并不罕见。

最后我忍不住演示了如何配置您的 ANT 构建以安装缺少的 ivy jar,从而使您的构建更加独立。

例子

build.xml

<project name="demo" default="forbidden-checks" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:fa="antlib:de.thetaphi.forbiddenapis">

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

  <target name="resolve" depends="install-ivy">
    <ivy:cachepath pathid="classpath">
      <dependency org="de.thetaphi" name="forbiddenapis" rev="2.2" />
    </ivy:cachepath>

    <ivy:cachepath pathid="all-lib-classpath">
      <dependency .... />
      <dependency .... />
      <dependency .... />
    </ivy:cachepath>
  </target>

  <target name="forbidden-checks" depends="resolve">

    <taskdef uri="antlib:de.thetaphi.forbiddenapis" classpathref="classpath"/>

    <fa:forbiddenapis classpathref="all-lib-classpath" dir="${build.dir}" targetVersion="${javac.version}">
      <bundledsignatures name="jdk-unsafe"/>
      <bundledsignatures name="jdk-deprecated"/>
      <bundledsignatures name="jdk-non-portable"/>
    </fa:forbiddenapis>
  </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>

</project>