Skip to content

Gradle Plugin

jte provides a Gradle 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 gradle plugin version matches the jte dependency version. You can create a jteVersion property in gradle.properties to sync the versions easily.

The plugin provides two tasks:

precompileJte task

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

plugins {
    id 'java'
    id 'gg.jte.gradle' version '${jteVersion}'
}

dependencies {
    implementation('gg.jte:jte:${jteVersion}')
}

jte {
    precompile()
}
plugins {
    java
    id("gg.jte.gradle") version("${jteVersion}")
}

dependencies {
    implementation("gg.jte:jte:${jteVersion}")
}

jte {
    precompile()
}

Task inputs

The follow inputs will be picked by the precompileJte task:

Parameter Description Default
sourceDirectory The directory where template files are located src/main/jte
targetDirectory The directory where compiled classes should be written to jte-classes
compilePath The compile-classpath to use sourceSets.main.runtimeClasspath
contentType The content type of all templates. Either Plain or Html Html
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. None
kotlinCompileArgs Sets additional compiler arguments for kte templates None
packageName The package name, where template classes are generated to Constants.PACKAGE_NAME_PRECOMPILED

About htmlPolicyClass

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

Clean all before precompiling

The precompileJte task 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.

generateJte task

This task generates all template classes in a sources directory. This only generates .java/.kt files, but does not compile them to .class files.

plugins {
    id 'java'
    id 'gg.jte.gradle' version '${jte.version}'
}

dependencies {
    implementation('gg.jte:jte:${jte.version}')
}

jte {
    generate()
}
plugins {
    java
    id("gg.jte.gradle") version("${jte.version}")
}

dependencies {
    implementation("gg.jte:jte:${jte.version}")
}

jte {
    generate()
}

Task Inputs

The follow inputs will be picked by the generateJte task:

Parameter Description Default
sourceDirectory The directory where template files are located src/main/jte
targetDirectory Destination directory to store generated templates. ${project.build.directory}/generated-sources/jte
contentType The content type of all templates. Either Plain or Html Html
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

Currently, the following extensions exist:

gg.jte.models.generator.ModelExtension

See details about it in the jte-models documentation.

Properties

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
plugins {
    id 'gg.jte.gradle' version '${jte.version}'
}

dependencies {
    implementation 'gg.jte:jte-runtime:${jte.version}'
    jteGenerate 'gg.jte:jte-models:${jte.version}'
}

jte {
    generate()

    // Remove the properties that don't make sense to your project
    jteExtension('gg.jte.models.generator.ModelExtension') {
        // Target language ("Java" and "Kotlin" are supported). "Java" is the default.
        language = 'Java'

        // Annotations to add to generated interfaces and classes
        interfaceAnnotation = '@foo.bar.MyAnnotation'
        implementationAnnotation = '@foo.bar.MyAnnotation'

        // Patterns to include (or exclude) certain templates
        includePattern = '\.pages\..*'
        excludePattern = '\.components\..*'
    }
}
plugins {
    id("gg.jte.gradle") version "${jte.version}"
}

dependencies {
    implementation("gg.jte:jte-runtime:${jte.version}")
    jteGenerate("gg.jte:jte-models:${jte.version}")
}

jte {
    generate()

    // Remove the properties that don't make sense to your project
    jteExtension("gg.jte.models.generator.ModelExtension") {
        // Target language ("Java" and "Kotlin" are supported). "Java" is the default.
        property("language", "Java")

        // Annotations to add to generated interfaces and classes
        property("interfaceAnnotation", "@foo.bar.MyAnnotation")
        property("implementationAnnotation", "@foo.bar.MyAnnotation")

        // Patterns to include (or exclude) certain templates
        property("includePattern", "\.pages\..*")
        property("excludePattern", '\.components\..*")
    }
}

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
plugins {
    id 'gg.jte.gradle' version '${jte.version}'
}

dependencies {
    implementation 'gg.jte:jte-runtime:${jte.version}'
    jteGenerate 'gg.jte:jte-models:${jte.version}'
}

jte {
    generate()
    jteExtension('gg.jte.nativeimage.NativeResourcesExtension')
}
plugins {
    id("gg.jte.gradle") version "${jte.version}"
}

dependencies {
    implementation("gg.jte:jte-runtime:${jte.version}")
    jteGenerate("gg.jte:jte-models:${jte.version}")
}

jte {
    generate()
    jteExtension("gg.jte.nativeimage.NativeResourcesExtension")
}

Sample project

There's an example Gradle test project using native-image compilation.