Ant在Build下载一个Jar

时间:2016-08-29 13:27:32

标签: java ant ivy

我正在尝试将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.

2 个答案:

答案 0 :(得分:1)

您需要为常春藤中的 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

答案 1 :(得分:1)

文件无法下载到您的工作区。 cachpath任务将完成两件事,将jar下载和缓存到默认目录&#34;〜/ .ivy2 / cache&#34;然后根据这些缓存的jar创建一个Ant路径。

其次,正如@Denis Kurochkin指出的那样,你使用的任务显然需要声明一个名称空间,而现代Ant任务并不罕见。

最后,我无法抗拒演示你如何配置你的ANT版本来安装常春藤罐子,如果它丢失了,使你的构建更加独立。

实施例

的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>