Zappabark Zappabark - 9 months ago 51
Groovy Question

Groovy Template Parsing Error: Unknown type: IMPORT

I am trying to process a very simple template through a groovy.text.StreamingTemplateEngine (Groovy v2.4.7). Here is the entire contents of the template file:

<% import org.yaml.snakeyaml.Yaml %>

I know this won't produce any output. I'm just trying to get the import to work.

I get this error:

Caught: groovy.text.TemplateParseException: Template parse error 'Unknown type: IMPORT at line: 1 column: 146. File: StreamingTemplateScript1.groovy ' at line 1, column 4
--> 1: <% import org.yaml.snakeyaml.Yaml %>

Here is the groovy code that's doing the processing:

#!/usr/bin/env groovy

def engine = new groovy.text.StreamingTemplateEngine()
def tmplt

if (args.length == 1) {
tmplt = engine.createTemplate(new File(args[0])).make()
else {
tmplt = engine.createTemplate(new BufferedReader(new InputStreamReader(

println tmplt.toString()

From what I understand of Groovy templates, including the import in the groovy script that processes the template won't work (tried that, too). I've seen others (apparently) successfully do what I'm trying to do.

What am I doing wrong?

Note that I am doing this outside of grails.


Answered my own question. According to the Groovy docs for the StreamingTemplateEngine:

This engine has equivalent functionality to the SimpleTemplateEngine but creates the template using writable closures making it more scalable for large templates.

This is apparently not true, as it does not understand import. To import libraries inside a Groovy template, you must use SimpleTemplateEngine instead.

I figured this out by setting up Apache Tomcat and Groovy Server Pages (w/o grails) to see if GSPs could do imports. They can. GSP uses, which uses the SimpleTemplateEngine.