I’m trying to write kotlin view engine for Spring MVC. I’m write custom ViewResolver and View, spring successfully calls them. In view I want to start Kotlin Script Engine:
class KotlinView constructor(path: String) : View {
...
override fun render(model: MutableMap<String, *>?, request: HttpServletRequest, response: HttpServletResponse) {
val classLoader = Thread.currentThread().getContextClassLoader();
val compilableEngine = ScriptEngineManager(classLoader).getEngineByName("kotlin") as Compilable
...
}
}
If I run app from IDEA, it works fine (Debug configuration - Gradle with bootRun gradle task). So, no problem here.
But if I run from java -jar /path/to/my/app.war
, expression ScriptEngineManager(classLoader).getEngineByName("kotlin")
returns null, and kotlin exception occurs because cannot cast null to Compilable.
In my build.gradle.kts
I added this:
tasks.withType<BootWar> {
requiresUnpack("**/kotlin-*.jar")
requiresUnpack("**/kotlinx-*.jar")
}
These files are marked with comment in war, and appears in my user temporary folder when app started via java -jar ...
.
I’m trying with kotlin-compiler and kotlin-scripting-jsr223-embeddable, behaviour is same.
I think that is about ClassLoaderProblems, but I can’t understand what went wrong. How to help ScriptEngineManager to find KotlinEngine in temporary folder? Or another solution?
UPD. I created repository with example. See KotlinView class, all requests are being processed with this class for now.
PS. My stackoverflow question