0

this did help me quite alot for debugging Groovy scripts when invoked via GroovyShell. However, this still only works, when the script is loaded via fully qualified file name (e.g. file:///home/user/x/src/main/groovy/package/File.groovy).

When I use

URL url = Thread.currentThread().getContextClassLoader().getResource("src/main/groovy/package/File.groovy");
shell.parse(new File(url.toURI));

it fails (CompilerConfiguration with debug enabled is set beforehand).

So, my scripts reside in the classpath and have packages assigned. They execute with both (via File directly and via File+Classloader resource) - but IDE debugging only works when referring to it as file resource (relative! so with "wrong" src/main/groovy/ prefix.

FYI - the scripts should later be included in JAR file and entire application will be execuded as a WAR.

Any hints on what I can try?

Thanks and best regards, Timo

Community
  • 1
  • 1
Timo Böwing
  • 313
  • 1
  • 2
  • 8

1 Answers1

1

So, no matter what I tried (between File, URL and URI - with and without direct Classloader usage) failed in at least one case:

  • WAR runtime (when being in dependent JAR)
  • Unit test launched from Maven
  • Unit test launched from IDE

Now, the only way I managed for all three cases is now direct usage of GroovyCodeSource:

CompilerConfiguration cc = CompilerConfiguration.DEFAULT   
cc.setDebug(true)    
def shell = new GroovyShell(
    Thread.currentThread().getContextClassLoader(), 
    binding, 
    cc
)

scriptUrl = Thread.currentThread().getContextClassLoader().getResource(it);   
GroovyCodeSource gcs = new GroovyCodeSource(scriptUrl);

Script s = shell.parse(gcs);
s.run();
Link64
  • 718
  • 4
  • 20
Timo Böwing
  • 313
  • 1
  • 2
  • 8