簡體   English   中英

即時編譯常規類

[英]compile groovy classes on the fly

我有一個gradle主項目,其中有兩個子項目,一個是java,另一個是groovy庫。

java項目是使用groovy庫的http服務器。

所有的程序都可以編譯,並且運行良好,但是我要嘗試的是在每個HTTP請求中即時編譯groovy庫,這樣我就不必在對groovy庫進行更改時重新編譯所有內容。

這可能嗎?

它正在使用GroovyClassLoader。 在處理靜態類字段和交叉引用時,我遇到了一些警告,但是我基本上在幾個項目中使用了此設置。 在某些情況下,您可能必須注意加載順序。

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