Ant Download a Jar at Build

我怕爱的太早我们不能终老 提交于 2019-12-12 19:29:33

问题


I'm trying to integrate forbiddenapis check into my project. I've defined that:

<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 includes all files to be checked by forbiddenapis plugin. I think that forbiddenapis jar will go into ${build.dir}. However I get that error:

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.

回答1:


The files don't get downloaded into your workspace. The cachpath task will do two things, download and cache jars into the default directory "~/.ivy2/cache" and then create an Ant path based on those cached jars.

Secondly, as @Denis Kurochkin pointed out, the task you're using apparently requires a namespace to be declared, not unusual with modern Ant tasks.

Finally I couldn't resist demonstrating how you can also configure your ANT build to install the ivy jar if it is missing, making your build even more stand-alone.

Example

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>



回答2:


You need to declare namespace for forbiddenapis task from Ivy:

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

Or declare task name explicitly:

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

Anyway look at the documentation https://github.com/policeman-tools/forbidden-apis/wiki/AntUsage



来源:https://stackoverflow.com/questions/39207497/ant-download-a-jar-at-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!