antlr / stringtemplate4

StringTemplate 4
http://www.stringtemplate.org
Other
956 stars 231 forks source link

import fails when template group is loaded using a container classloader #191

Closed lkrylov closed 6 years ago

lkrylov commented 7 years ago

Stringtemplate fails to load an included resource in application containers with custom class loaders using import statement in the STG file:

import "org/foo/templates/g.stg"

The particular framework I encountered the issue with was Spring Boot

The underlying cause is that the Misc.urlExists(URL url) method creates a new URLClassLoader() instance and uses the findResource method to check for imported resource. This does not work with the URLs generated by org.springframework.boot.loader.LaunchedURLClassLoader.

I'm attaching a patch to simplify the Misc.urlExists implementation. Mostly it's a revert of the changes made to address #43 and include an alternative workaround for the JRE bug that causes #43. The tests introduced as a part of #43 still pass with the proposed patch.

patch.txt

drealeed commented 6 years ago

we're having the same problem. I have a spring boot jar that includes a dependency jar which includes the templates. The "import HTMLShared.stg" statement in the templates is failing in Misc.urlExists, even though it does exist; a call to

Thread.currentThread().getContextClassLoader().getResource("full jar path/HTMLShared.stg")

in Misc.urlExists() does return the file.

This has been open for over a year; are there any plans to merge the change, or deploy an update of ST4?