0

I have a groovy script (source.groovy) that needs to call a method from another groovy script (external.groovy). The problem is external.groovy imports a library that does not exists so I get an error. Here is an example:

Source.groovy:

 new GroovyShell().parse( new File( 'external.groovy' ) ).with {
    method()
  }

Here is external.groovy:

import com.foo.doesnotexsist
def method() {println "test"}

When I run Source.groovy I get an error because com.foo.doesnotexsist does not exist. I don't care that it does not exists because it does not effect the method() function. Is there a way I can call the method() function?

user2475310
  • 733
  • 4
  • 13
  • 19

1 Answers1

0

Maybe it is not the way we want to achieve that, but there is simple solution to remove unwanted imports:

def text = new File( 'external.groovy' ).findAll{!(it =~ /^\s*import/)}.join('\n')
new GroovyShell().parse( text ).with{method()}
hexin
  • 947
  • 7
  • 17