ant:从<script>调用antcall会抛出java.lang.NullPointerException </script>

时间:2012-09-05 11:19:50

标签: ant

在ant 1.8.2构建系统中,我有一个依赖<antcall/>的javascript。这导致我java.lang.NullPointerException指向antcall。

脚本:

<project default="main">
    <target name="main">
        <script language="javascript"> <![CDATA[
        task = project.createTask( 'macro' );
        task.execute();
        ]]></script>
    </target>

    <macrodef name="macro">
        <sequential>
            <antcall target="antcall" />
        </sequential>
    </macrodef>

    <target name="antcall">
        <echo>[antcall] succeed</echo>
    </target>

</project>

build.xml:11:java.lang.NullPointerException(#3 ...)。堆栈跟踪只是谈论RhinoScriptEngine,不确定是否有任何帮助

at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:153)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:167)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.util.ReflectUtil.invoke(ReflectUtil.java:108)
at org.apache.tools.ant.util.ReflectWrapper.invoke(ReflectWrapper.java:81)
at org.apache.tools.ant.util.optional.JavaxScriptRunner.evaluateScript(JavaxScriptRunner.java:103)
at org.apache.tools.ant.util.optional.JavaxScriptRunner.executeScript(JavaxScriptRunner.java:67)
at org.apache.tools.ant.taskdefs.optional.Script.execute(Script.java:52)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
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:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
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:809)
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)

我的问题是: - 有人可以重现这个问题吗? - 我做错了什么吗? - 任何蚂蚁专家帮助我: - )

1 个答案:

答案 0 :(得分:3)

我在Groovy中发现了一个错误报告,详细说明了类似问题http://jira.codehaus.org/browse/GROOVY-1506attached patch by Andreas Sahlbach通过将所有者分配给新创建的任务来修复groovy问题。

所以我继续前进并补充道:

task.setOwningTarget( self.getOwningTarget() );

解决了这个问题。我已将原始脚本和固定脚本放在gist 3636007中,以便其他人可以使用。

最终脚本如下:

<project default="main">
    <target name="main">
        <script language="javascript"> <![CDATA[
        task = project.createTask( 'macro' );

        if( task.getOwningTarget() == null ) {
            task.log( "Assigning an owner ..." );
            task.setOwningTarget( self.getOwningTarget() );
            task.log( "Task:  " + task.getOwningTarget() );
        }

        try {
            task.execute();
        } catch(err) {
            task.log( "Execution error: " + err.message );
        }

        ]]></script>
    </target>

    <macrodef name="macro">
        <sequential>
            <antcall target="antcall" />
        </sequential>
    </macrodef>

    <target name="antcall">
        <echo>[antcall] succeed</echo>
    </target>

</project>

结果执行是:

$ ant
Buildfile: /Users/amusso/ant/bug/build.xml

main:
    [macro] Assigning an owner ...
    [macro] Task:  main

antcall:
     [echo] [antcall] succeed

BUILD SUCCESSFUL
Total time: 0 seconds

\ O /