我正在尝试将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罐会进入${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.
文件不会被下载到您的工作区中。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>
标题>
您需要从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