常春藤清除缓存任务抛出NullPointErexception

时间:2012-10-02 12:16:22

标签: java ant ivy slf4j

我有以下项目目录结构:

MyProject/
    src/main/java/
        All of my Java sources
    build/
        build.xml
        build.properties
        ivy.xml
        ivy-settings.xml
        ivy-settings.properties

build.xml看起来像这样:

<project name="MyProject" default="audit" basedir=".." xmlns:ivy="antlib:org.apache.ivy.ant">
    <property file="build/build.properties"/>
    <property environment="env"/>

    <path id="ant.lib.path">
        <fileset dir="${env.ANT_HOME}/lib" includes="*.jar"/>
    </path>

    <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ant.lib.path"/>

    <target name="configIvy">
        <echo message="Configuring Ivy."/>
        <echo message="URL is: ${ivy.settings.home}"/>
        <ivy:settings url="${ivy.settings.home}"/>

        <!-- Clear/flush the Ivy cache. -->
        <echo message="Cleaning the local Ivy cache for the current build."/>
        <ivy:cleancache/>
    </target>
</project>

当我运行ant -buildfile /<path-to-my-project>/MyProject/build/build.xml configIvy时,我得到以下控制台输出:

Buildfile: /<path-to-my-project>/MyProject/build/build.xml

configIvy:
    [echo] Configuring Ivy.
    [echo] URL is: file:////<path-to-my-project>/MyProject/build/ivy-settings.xml
[ivy:cleancache] :: Apache Ivy 2.3.0-rc1 - 20120416000235 :: http://ant.apache.org/ivy/ ::

BUILD FAILED
/<path-to-my-project>/MyProject/build/build.xml:85: java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.apache.ivy.util.url.URLHandlerRegistry.getHttp(URLHandlerRegistry.java:47)
    at org.apache.ivy.ant.IvyAntSettings.configureURLHandler(IvyAntSettings.java:367)
    at org.apache.ivy.ant.IvyAntSettings.createIvyEngine(IvyAntSettings.java:267)
    at org.apache.ivy.ant.IvyAntSettings.getConfiguredIvyInstance(IvyAntSettings.java:237)
    at org.apache.ivy.ant.IvyTask.getIvyInstance(IvyTask.java:92)
    at org.apache.ivy.ant.IvyTask.prepareTask(IvyTask.java:256)
    at org.apache.ivy.ant.IvyTask.execute(IvyTask.java:276)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:392)
    at org.apache.tools.ant.Target.performTasks(Target.java:413)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:811)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.NullPointerException
    at org.apache.log4j.Category.isDebugEnabled(Category.java:129)
    at org.apache.commons.logging.impl.Log4JLogger.isDebugEnabled(Log4JLogger.java:239)
    at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:69)
    ... 25 more

我的ivy-settings.xml文件指定了我本地计算机(http://localhost:8080/artifactory/myrepo)上托管的Artifactory仓库的URL解析程序。我想知道Ivy是否在引擎盖下使用HttpClient(如堆栈跟踪所示),并且出于某种原因,因为它在同一台机器上的HTTP URL而窒息?也许?!?并且,我确信在运行Ant构建时URL正确并且Artifactory正在运行!

有人能发现这里发生了什么吗?为什么<ivy-cleancache>会抛出NPE?我正在查看它的源代码,似乎无法找到NPE的来源或原因。如果需要,我可以提供更多细节。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我以为我对此作了回应,但我在这里看不到......

不要为您的项目添加额外的罐子$ANT_HOME/lib。这有几个原因:

  • 正如您所知,可能存在jar冲突,因为每组可选任务都会尝试设置所需的类路径。是的,当你执行<taskdef>时不必设置类路径是很好的,但它并没有那么糟糕。
  • 如果您将项目交给其他人,他们必须先安装所有可选的罐子才能进行构建。

更好的方法是创建一个${basedir}/ant.lib目录,然后将每组ant任务jar放在他们自己的子目录中。例如,你将常春藤罐放在${basedir}/ant.lib/ivy中,然后将Checkstyle罐子放在${basedir}/ant.lib/checkstyle中。然后,使用指向目录的类路径定义taskdef,如下所示:

 <taskdef resource="org/apache/ivy/ant/antlib.xml" 
      uri="antlib:org.apache.ivy.ant">
      <classpath>
          <fileset dir="${basedir}/lib/ivy"/>
      </classpath>
 </taskdef>

这样,常春藤不会拿起错误的罐子。作为奖励,你也可以给某人你的项目,而Ivy已经安装并运行了。他们不需要下载Ivy并在正确的类路径中设置jar。

顺便说一下,$ANT_HOME/lib已经在Ant类路径中了,所以如果你没有指定类路径,$ANT_HOME/lib中的所有jar都会被自动拾取。你可以简单地做到这一点:

 <taskdef resource="org/apache/ivy/ant/antlib.xml" 
      uri="antlib:org.apache.ivy.ant"/>

不需要类路径。