Ant引发现有Jar文件的引用错误

时间:2016-08-07 14:33:21

标签: ant

我想在build.xml文件中添加任务:

<target name="forbidden-checks" depends="download-forbidden-checks">
    <taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
             classpathref="lib/forbiddenapis-2.2.jar"/>
    <fa:forbiddenapis classpathref="build.classpath" dir="${build.dir}" targetVersion="${jdk.version}">
        <bundledsignatures name="jdk-unsafe"/>
        <bundledsignatures name="jdk-deprecated"/>
        <bundledsignatures name="jdk-non-portable"/>
    </fa:forbiddenapis>
</target>

我看到jar已下载,没有问题。但是我收到一个错误:

download-forbidden-checks:

setup-maven-url:

download-via-maven:
      [get] Getting: https://repo1.maven.org/maven2/de/thetaphi/forbiddenapis/2.2/forbiddenapis-2.2.jar
      [get] To: /home/kamaci/pro/lib/forbiddenapis-2.2.jar

forbidden-checks:

BUILD FAILED
/home/kamaci/pro/build.xml:2391: Reference /home/kamaci/pro/lib/forbiddenapis-2.2.jar not found.

我检查了jar的路径是否有问题。所以,我把forbiddenapis-2.2.jar放到了另一个地方,而不是项目。我从我的taskef指出了那个罐子,但我得到了同样的错误。

有什么想法吗?

编辑1:

我已将其更改为:

<target name="forbidden-checks" depends="download-forbidden-checks">
    <taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
        <classpath>
            <pathelement location="/home/kamaci/pro/forbiddenapis-2.2.jar"/>
        </classpath>
    </taskdef>
    <fa:forbiddenapis targetVersion="${jdk.version}">
        <classpath>
            <pathelement location="lib/forbiddenapis-2.2.jar"/>
        </classpath>
        <bundledsignatures name="jdk-unsafe"/>
        <bundledsignatures name="jdk-deprecated"/>
        <bundledsignatures name="jdk-non-portable"/>
    </fa:forbiddenapis>
</target>

然而,它没有用:

Problem: failed to create task or type antlib:de.thetaphi.forbiddenapis: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.
No types or tasks have been defined in this namespace yet

This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -/usr/share/ant/lib
        -/home/kamaci/.ant/lib
        -a directory added on the command line with the -lib argument

当我运行时:

ant forbidden-checks -lib lib/

它有效。我的项目定义从build.xml开始:

为什么我要添加-lib?有什么方法可以避免吗?

1 个答案:

答案 0 :(得分:0)

非常确定问题是classpathref因为它使用的是文字值而不是路径元素,所以jar可能不存在于类路径中,尝试创建一个路径元素用于{{3 }}

<path id="lib.path">
    <fileset dir="lib" includes="lib/*.jar"/>
</path>

<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask"
         classpathref="lib.path"/>

或在taskdef中定义类路径。

<taskdef name="forbidden-apis" classname="de.thetaphi.forbiddenapis.AntTask">
    <classpath>
        <pathelement location="lib/forbiddenapis-2.2.jar"/>
    </classpath>
</taskdef>