Skip to content

Commit

Permalink
docs: Add Maven Plugin documentation (#341)
Browse files Browse the repository at this point in the history
* docs: Add new docs page for Maven Plugin

* docs: remove unnecessary link entry

* docs: use predictable heading ids

* docs: spelling and grammar improvements

Co-authored-by: Niklas Keller <[email protected]>

---------

Co-authored-by: Niklas Keller <[email protected]>
  • Loading branch information
marcospereira and kelunik authored Apr 9, 2024
1 parent 943b854 commit 0f73648
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 34 deletions.
4 changes: 2 additions & 2 deletions docs/binary-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: How to use binary rendering for max throughput

Available since jte ^^**1.7.0**^^.

Most template parts are static content, and only a few parts of a template are dynamic. Encoding those static parts repeatedly on every request is wasteful if your web framework sends binary UTF-8 content to the user. jte makes it is possible to encode those static parts when the template is compiled:
Most template parts are static content, and only a few parts of a template are dynamic. Encoding those static parts repeatedly on every request is wasteful if your web framework sends binary UTF-8 content to the user. jte makes it possible to encode those static parts when the template is compiled:

=== "Java (Application run time)"

Expand All @@ -29,7 +29,7 @@ Most template parts are static content, and only a few parts of a template are d

This generates a binary content resource for each template. Those pre-encoded UTF-8 `#!java byte[]` arrays are loaded in memory from the resource file together with the template class. This also implies that the constant pool is released from holding template strings.

To fully utilize binary templates, you must use a binary template output, like [`gg.jte.Utf8ByteOutput`](https://www.javadoc.io/doc/gg.jte/jte-runtime/{{ latest-git-tag }}/gg.jte.runtime/gg/jte/output/Utf8ByteOutput.html). This output is heavily optimized to consume as little CPU and memory as possible when using binary templates.
To fully benefit from binary templates, you must use a binary template output, like [`gg.jte.Utf8ByteOutput`](https://www.javadoc.io/doc/gg.jte/jte-runtime/{{ latest-git-tag }}/gg.jte.runtime/gg/jte/output/Utf8ByteOutput.html). This output is heavily optimized to consume as little CPU and memory as possible when using binary templates.

!!! info

Expand Down
2 changes: 1 addition & 1 deletion docs/html-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Will render this HTML:

jte does not render HTML, CSS and JavaScript comments. You can use the natural comment syntax without worrying about leaking information/data to the outside.

## HTML Escaping
## HTML Escaping { #html-escaping }

Output escaping depends on the [`gg.jte.ContentType`](https://www.javadoc.io/doc/gg.jte/jte-runtime/{{ latest-git-tag }}/gg.jte.runtime/gg/jte/ContentType.html) the engine is created with:

Expand Down
27 changes: 7 additions & 20 deletions docs/jte-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A generator extension for jte which creates a typesafe facade for r

# jte-models Facade Generator

jte-models is a generator extension for jte which creates a typesafe facade for rendering templates.
jte-models is a generator extension for jte that creates a typesafe facade for rendering templates.

## Setup

Expand All @@ -19,29 +19,11 @@ To use jte-models, set up your build script to include one of these:
<artifactId>jte-maven-plugin</artifactId>
<version>${jte.version}</version>
<configuration>
<sourceDirectory>${basedir}/src/main/jte</sourceDirectory>
<sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
<contentType>Html</contentType>
<extensions>
<extension>
<className>gg.jte.models.generator.ModelExtension</className>
<!-- optional settings to configure the target language ("Java" and "Kotlin" are supported):
<settings>
<language>Java</language>
</settings>
-->
<!-- optional settings to include annotations on generated classes:
<settings>
<interfaceAnnotation>@foo.bar.MyAnnotation</interfaceAnnotation>
<implementationAnnotation>@foo.bar.MyAnnotation</implementationAnnotation>
</settings>
-->
<!-- optional settings to include (or exclude) certain templates.
The value is a regular expression matched against the class name of the generated JTE class.
<settings>
<includePattern>\.pages\..*</includePattern>
<excludePattern>\.components\..*</excludePattern>
</settings>
-->
</extension>
</extensions>
</configuration>
Expand Down Expand Up @@ -133,7 +115,12 @@ To use jte-models, set up your build script to include one of these:

Run the build to generate classes.

!!! tip "Maven configuration"

See details about how to fully configure the extension in the [Maven plugin documentation](maven-plugin.md#model-extension).

## Output

Additional generated classes will include a facade interface named `gg.jte.generated.precompiled.Templates`, with implementations:

- `gg.jte.generated.precompiled.StaticTemplates`
Expand Down
222 changes: 222 additions & 0 deletions docs/maven-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
---
title: Maven Plugin
description: How to use the jte Maven plugin.
---

jte provides a [Maven Plugin][jte-maven-compiler-plugin] that you can integrate in your build process to either [precompile templates](pre-compiling.md), or to generate the Java/Kotlin code for your templates.

!!! tip "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 { #precompile-goal }

See [Precompiling Templates](pre-compiling.md) for more information. To use and configure this goal, you would use:

```xml linenums="1"
<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 { #precompile-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`][html-policy] 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](binary-rendering.md) 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`][constants-package-name] |
| `keepGeneratedSourceFiles` | If it should keep all generated jte source files after compilation | `false` |

!!! info "About `htmlPolicyClass`"

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

!!! info "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`:

```xml linenums="1"
<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:

```xml linenums="1"
<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:

- [Maven Compiler Plugin](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#parameter-details)
- [`-release` option](https://docs.oracle.com/en/java/javase/21/docs/specs/man/javac.html#option-release)
- [`-source`](https://docs.oracle.com/en/java/javase/21/docs/specs/man/javac.html#option-source) and [`-target`](https://docs.oracle.com/en/java/javase/21/docs/specs/man/javac.html#option-target) options

!!! warning "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](hot-reloading.md).

## `generate` goal { #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.

```xml linenums="1"
<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 { #generate-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](binary-rendering.md) resource for each template | `false` |
| `packageName` | The package name, where template classes are generated to | [`Constants.PACKAGE_NAME_PRECOMPILED`][constants-package-name] |
| `targetResourceDirectory` | Directory in which to generate non-java files (resources) | None |
| `extensions` | Extensions this template engine should load | None |

### Extensions { #generate-extensions }

Currently, the following extensions exist:

#### `gg.jte.models.generator.ModelExtension` { #model-extension }

See details about it in the [jte-models documentation](jte-models.md).

##### Parameters { #model-extension-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 { #model-extension-example }

```xml linenums="1"
<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` { #native-resources-extension }

See details about it in the [native-image documentation](pre-compiling.md#graalvm-native-image-support). This extension requires no parameters.

##### Example { #native-resources-extension-example }

```xml linenums="1"
<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>
```

[jte-maven-compiler-plugin]: https://search.maven.org/artifact/gg.jte/jte-maven-plugin
[html-policy]: https://www.javadoc.io/doc/gg.jte/jte-runtime/{{ latest-git-tag }}/gg.jte.runtime/gg/jte/html/HtmlPolicy.html
[constants-package-name]: https://www.javadoc.io/doc/gg.jte/jte-runtime/{{ latest-git-tag }}/gg/jte.runtime/gg/jte/Constants.html#PACKAGE_NAME_PRECOMPILED
Loading

0 comments on commit 0f73648

Please sign in to comment.