Hi,

One of the companies i work for has moved to Google Artifact Registry (GAR) which does not work well with @Grab so I am attempting to write an alternative that uses maven (with some extensions handling GAR communication) for dependency resolution.

My problem is that in order for this to work, I have to run the dependency grabbing code first and then the groovy script. e.g. like this

String depScript ='''def resolver = new se.alipsa.groovy.resolver.DependencyResolver(this) resolver.addDependency('com.googlecode.libphonenumber:libphonenumber:8.13.26') ''' String script ='''import com.google.i18n.phonenumbers.PhoneNumberUtildef phoneNumber = PhoneNumberUtil.getInstance().parse('+46 70 12 23 198', 'SE') println "Phone number is ${numberUtil.isValidNumber(phoneNumber) ? '' : 'NOT '}valid" '''

GroovyScriptEngineImpl groovyScriptEngine =new GroovyScriptEngineImpl()
groovyScriptEngine.eval(depScript)
groovyScriptEngine.eval(script)

I tried to turn this into an annotation and adding the dependency code in a 
static initializer using an ASTTransformation but import statements
are processed before static initializers during compilation so this will not work. What 
maybe would work would be to register some "bootstrap" script/class
that is executed before the actual script is processed and adds the 
dependencies to the ClassLoader before processing the script
(essentially running the depScript code).

Then the above could be simplified to:

String script ='''@Lib('com.googlecode.libphonenumber:libphonenumber:8.13.26')import com.google.i18n.phonenumbers.PhoneNumberUtildef phoneNumber = PhoneNumberUtil.getInstance().parse('+46 70 12 23 198', 'SE') println "Phone number is ${numberUtil.isValidNumber(phoneNumber) ? '' : 'NOT '}valid" '''
i.e. I would only need to change @Grab statements to @Lib statements (@Lib is 
the annotation pointing to the GroovyASTTranformation class that would add the 
code to the
"bootstrap" class).

Is there a way to do such a thing?

Regards,
Per

Reply via email to