简体   繁体   English

即时编译常规类

[英]compile groovy classes on the fly

I have a gradle main project with two sub-projects, one is java and the other is a groovy library. 我有一个gradle主项目,其中有两个子项目,一个是java,另一个是groovy库。

The java project is an http server that uses the groovy libraries. java项目是使用groovy库的http服务器。

All compiles, and runs well but what I'm trying is to compile the groovy libraries on the fly (on each http request), so that I don't have to recompile everything whenever I make changes to the groovy libraries. 所有的程序都可以编译,并且运行良好,但是我要尝试的是在每个HTTP请求中即时编译groovy库,这样我就不必在对groovy库进行更改时重新编译所有内容。

Is this possible? 这可能吗?

It is using GroovyClassLoader. 它正在使用GroovyClassLoader。 There are some caveats that I've run into when dealing with Static class fields and cross-references, but I basically use this setup on a couple of my projects. 在处理静态类字段和交叉引用时,我遇到了一些警告,但是我基本上在几个项目中使用了此设置。 You may have to watch loading order in some cases. 在某些情况下,您可能必须注意加载顺序。

def groovyClassLoader = new GroovyClassLoader()

def classPaths = [ '/opt/myProject/src/groovy/' ]

// First, add Class Paths -- these are the root directories of your code files.
for (String path in classPaths) {
    File dir = new File(path)
    groovyClassLoader.addClasspath(dir.getAbsolutePath())
}

def src = [ '/opt/myProject/src/groovy/net/me/program/' ] 

// Now, load groovy files
for (String path in src) {
    // Iterate differently if no access to FileUtils
    File[] directoryListing = FileUtils.listFiles(new File(path), null, false)
    if (directoryListing != null) {
        for (File child in directoryListing) {
            groovyClassLoader.parseClass(child)
        }
    }
}

// See all the loaded classes
println(groovyClassLoader.loadedClasses)

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

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