Skip to content

Maven Plugin

jte provides a Maven Plugin that you can integrate in your build process to either precompile templates, or to generate the Java/Kotlin code for your templates.

Versions alignment

Make sure the jte maven plugin version matches the jte dependency version. You can create a ${jte.version} property to sync the versions easily.

The plugin provides two goals:

precompile goal

See Precompiling Templates for more information. To use and configure this goal, you would use:

<plugin>
    <groupId>gg.jte</groupId>
    <artifactId>jte-maven-plugin</artifactId>
    <version>${jte.version}</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
        <targetDirectory>${project.build.directory}/jte-classes</targetDirectory>
        <contentType>Html</contentType>
    </configuration>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>precompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Configurations

The default phase for this goal is process-classes, and it accepts the following configuration options:

Parameter Description Default
sourceDirectory The directory where template files are located None. Required configuration.
targetDirectory The directory where compiled classes should be written to None. Required configuration.
compilePath The compile-classpath to use project.compileClasspathElements
contentType The content type of all templates. Either Plain or Html None. Required configuration
trimControlStructures Trims control structures, resulting in prettier output false
htmlTags Intercepts the given html tags during template compilation None
htmlPolicyClass An HtmlPolicy used to check the parsed HTML at compile time None
htmlCommentsPreserved If HTML comments should be preserved in the output false
binaryStaticContent If to generate a binary content resource for each template false
compileArgs Sets additional compiler arguments for jte templates. See note below
kotlinCompileArgs Sets additional compiler arguments for kte templates None
packageName The package name, where template classes are generated to Constants.PACKAGE_NAME_PRECOMPILED
keepGeneratedSourceFiles If it should keep all generated jte source files after compilation false

About htmlPolicyClass

The htmlPolicyClass will default to gg.jte.html.OwaspHtmlPolicy if the content type is Html.

About compileArgs

This option default depends on the maven.compiler.release or, if it is absent, on maven.compiler.source and maven.compiler.target properties.

So, if the following property is configured in your pom.xml:

1
2
3
<properties>
    <maven.compiler.release>21</maven.compiler.release>
</properties>

The compileArgs will default to --release 21. If maven.compiler.release is not configured, it will try to use maven.compiler.source and maven.compiler.target instead:

1
2
3
4
<properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
</properties>

This will result in --source 21 --target 21 as the default value for compileArgs. If none of those properties are configured, then the default value will be empty. Release is usually preferred over source and target since it will also ensure the correct API versions are used. See more details in the links below:

Clean all before precompiling

The precompile goal cleans the directory containing the compiled template classes every time it runs. In your local development environment, it may make more sense to use hot reloading.

generate goal

This goal generates all template classes in a source directory. This only generates .java files, but does not compile them to .class files.

<plugin>
    <groupId>gg.jte</groupId>
    <artifactId>jte-maven-plugin</artifactId>
    <version>${jte.version}</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
        <contentType>Html</contentType>
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Configurations

The default phase for this goal is generate-sources, and it accepts the following configuration options:

Parameter Description Default
sourceDirectory The directory where template files are located None. Required configuration.
targetDirectory Destination directory to store generated templates. ${project.build.directory}/generated-sources/jte
contentType The content type of all templates. Either Plain or Html None. Required configuration
trimControlStructures Trims control structures, resulting in prettier output false
htmlTags Intercepts the given html tags during template compilation None
htmlCommentsPreserved If HTML comments should be preserved in the output false
binaryStaticContent If to generate a binary content resource for each template false
packageName The package name, where template classes are generated to Constants.PACKAGE_NAME_PRECOMPILED
targetResourceDirectory Directory in which to generate non-java files (resources) None
extensions Extensions this template engine should load None

Extensions

Currently, the following extensions exist:

gg.jte.models.generator.ModelExtension

See details about it in the jte-models documentation.

Parameters

The following parameters are available for this extension:

Parameter Description Default
interfaceAnnotation The FQCN of the annotation to add to the generated interface None
implementationAnnotation The FQCN of the annotation to add to the generated implementation classes None
language The target language for the generated classes. Either Java or Kotlin Java
includePattern A regular expression to only include certain templates None
excludePattern A regular expression to exclude certain templates None
Example
<plugin>
    <groupId>gg.jte</groupId>
    <artifactId>jte-maven-plugin</artifactId>
    <version>${jte.version}</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
        <contentType>Html</contentType>
        <extensions>
            <extension>
                <className>gg.jte.models.generator.ModelExtension</className>
                <settings>
                    <interfaceAnnotation>@foo.bar.MyAnnotation</interfaceAnnotation>
                    <implementationAnnotation>@foo.bar.MyAnnotation</implementationAnnotation>
                    <language>Java</language>
                    <includePattern>\.pages\..*</includePattern>
                    <excludePattern>\.components\..*</excludePattern>
                </settings>
            </extension>
        </extensions>
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

gg.jte.nativeimage.NativeResourcesExtension

Version note

Available since jte 1.10.0.

An application jar with generated classes can be built into a native binary using GraalVM native-image. To support this, this extension generates the necessary configuration files to tell native-image about classes loaded by reflection.

Example
<plugin>
    <groupId>gg.jte</groupId>
    <artifactId>jte-maven-plugin</artifactId>
    <version>${jte.version}</version>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
        <contentType>Html</contentType>
        <extensions>
            <extension>
                <className>gg.jte.nativeimage.NativeResourcesExtension</className>
            </extension>
        </extensions>
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>