Precompiling Templates¶
To speed up startup and rendering on your production server, it is possible to precompile all templates during the build. This way, the template engine can load each template's .class
file directly without first compiling it. For security reasons, you may not want to run a JDK on production - with precompiled templates, this is unnecessary. The recommended way to set up jte is to instantiate the engine differently, depending on when you are developing or running on a server.
To do this, you must create a gg.jte.TemplateEngine
with the createPrecompiled
factory method and specify where compiled template classes are located. Currently, there are two options available to do this.
Using a directory on your server (recommended)¶
You must deploy the precompiled templates to your server when using this method.
Maven¶
You can use a Maven plugin to precompile all templates during the Maven build. You would need to put this in build / plugins of your projects' pom.xml
. Please note that paths specified in Java must match those specified in Maven.
Warning
Make sure the jte maven plugin version matches the jte dependency version. You can create a ${jte.version}
to sync the versions easily.
Gradle¶
Version note
Available since jte 1.6.0.
The Gradle plugin can precompile all templates during the Gradle build. Please note that paths specified in Java must match those specified in Gradle.
Warning
Make sure the jte gradle plugin version matches the jte dependency version. You can create a jteVersion
in gradle.properties
to sync the versions easily.
In case you would like to build a self-contained JAR, you can add this to your build.gradle
:
And init the template engine like this for production builds:
This way, the templates are loaded from the application class loader. See this issue for additional information.
Using the application class loader¶
Version note
Available since jte 1.2.0.
When using this method, the precompiled templates are bundled within your application jar file. The plugin generates *.java
files for all jte templates during Maven's GENERATE_SOURCES
phase. Compilation of the templates is left to the Maven Compiler plugin.
While this provides you with a nice self-containing jar, it has some limitations:
- Some plugin settings are not supported, like configuring a custom HtmlPolicy class (this is because project classes are not yet compiled at the
GENERATE_SOURCES
phase).
Maven¶
You can use a Maven plugin to generate all templates during the Maven build. You would need to put this in build / plugins of your projects' pom.xml
. Please note that paths specified in Java must match those specified in Maven.
Warning
Make sure the jte maven plugin version matches the jte dependency version. You can create a ${jte.version}
to sync the versions easily.
Gradle¶
Version note
Available since jte 1.6.0.
The Gradle plugin can generate all templates during the Gradle build. Please note that paths specified in Java must match those specified in Gradle.
Warning
Make sure the jte gradle plugin version matches the jte dependency version. You can create a jteVersion
in gradle.properties
to sync the versions easily.
GraalVM native-image support¶
How to use
See details about how to configure your build tool to generate the necessary configuration files to tell native-image
about classes loaded by reflection:
Binary encoding¶
Use binary encoding for maximum performance! To activate it with precompiled templates, modify your build file.