简体   繁体   English

导入外部(未编译)的groovy类,并将其保留在jar中

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

I created a Groovy project in Eclipse. 我在Eclipse中创建了一个Groovy项目。 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. 我想构建一个可执行jar,将一些常规文件源保留在jar外,以便可以在运行时对其进行修改和重新加载。

I know I can do this using the GroovyScriptEngine: 我知道我可以使用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). 如果您的jar在groovy的上下文中,那么您可以将groovy文件(类)添加到classpath文件夹中。 groovy classloader compiles them automatically on access. groovy类加载器会在访问时自动编译它们。

otherwise create a groovy engine, add to classpath directory with groovy files.. 否则,创建一个groovy引擎,将groovy文件添加到classpath目录。

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. 如果将它们保留在jar之外,但位于类路径中,则必须将它们编译为.class文件,系统才能将它们拾取。

The best bet is to use the ScriptEngine you mentioned which "compiles" them internally (without saving the classes). 最好的选择是使用您提到的ScriptEngine在内部“编译”它们(而不保存类)。 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. 这使得.jar中的类很难灵活地访问外部groovy脚本,但这可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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