简体   繁体   中英

Import external (not compiled) groovy classes and keep them outside the jar

I created a Groovy project in Eclipse. I would like to build an executable jar, keeping some of my groovy file sources outside the jar, so they can be modified and reloaded at runtime.

I know I can do this using the GroovyScriptEngine:

Class modelClass = new GroovyScriptEngine( "path_to_file" ).loadScriptByName( "Model.groovy" ) ;
Object modelInstance = modelClass.newInstance() ;

Now, my question is: is it possible to simply, for instance inside my main class, use:

import Model

class Main {
static void main(String[] args) {
    Model model = new Model(args)
    ...
}

I hope my question is clear enough... And thank you so much for your help!

if your jar in context of groovy, then you can just add to classpath folder with groovy files(classes). groovy classloader compiles them automatically on access.

otherwise create a groovy engine, add to classpath directory with groovy files..

Something has to compile them. If you keep them outside the jar but in your classpath you'd have to have them compiled to .class files for the system to pick them up.

The best bet is to use the ScriptEngine you mentioned which "compiles" them internally (without saving the classes). This makes it a little difficult for classes in your .jar to flexibly access the external groovy scripts but that's probably for the best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM