-->

调试从Eclipse在的ScriptEngine运行Groovy脚本(Debugging Groov

2019-06-26 16:27发布

我有运行这样的Groovy脚本:

File scriptFile = ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
Object evalResult = engine.eval(script, bindings);

不出所料,在脚本文件的断点设置不会触发。 我能更改,使其工作? 脚本需要在较大的程序(没有单独的启动配置)的背景下运行,并通过ScriptEngine ,并且该文件仅在运行时是已知的。

Answer 1:

我使用这个技巧:我已经定义了一个Java类Debugger看起来像这样:

public class Debugger {

    private static final Logger log = LoggerFactory.getLogger( Debugger.class );

    /**
     * Invoke this method anywhere to end up in the Java debugger.
     * 
     * <p>Sometimes, you have code that might eventually be executed or you have a place
     * where you want to stop but no code line.
     * 
     * <p>In these case, use this method.
     * 
     * <p>Don't forget to set a breakpoint, first :-)
     * */
    public static void stopInDebugger() {
        log.error( "Please set a breakpoint in Debugger.stopInDebugger() and try again whatever you just did!" );
    }
}

我在一个断点log.error中的Ecipse线。

现在,我可以把这个行成剧本,我希望有一个断点:

Debugger.stopInDebugger();

当然,它不会让我轻松地通过脚本一步,但它总比没有好。



Answer 2:

在classpath中的源文件夹脚本文件(听起来像它不是)? 如果不是,是真的。 你也可以改变你的喜好,以确保脚本文件从未被编译器编译(有些情况下甚至不被复制到输出文件夹)。 转到首选项 - > Groovy中 - >编译器,并期待在脚本文件夹来实现这一目标。



文章来源: Debugging Groovy scripts running in a ScriptEngine from Eclipse