Java template generation

Write Maven properties into Java source code and automatically add the main source code or test source code compilation directory.

when to use

When it is necessary to write build information such as project version into Java constants, the template is saved in a dedicated directory, and the properties are filtered by the plugin during construction. Handwritten code and generated code are maintained separately to avoid directly modifying the results in target/.

Configure plugin

The following configuration is placed in <build><plugins> of application pom.xml:

<plugin>
    <groupId>com.lodsve.maven.plugins</groupId>
    <artifactId>lodsve-javatemplate-maven-plugin</artifactId>
    <version>1.0.3</version>
    <executions>
        <execution>
            <id>generate-java</id>
            <goals>
                <goal>generate-sources</goal>
                <goal>generate-test-sources</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Set project.build.sourceEncoding to UTF-8 in <properties> of POM. Both goals have declared default life cycle phases, and there is no need to manually specify phases repeatedly.

a version constant template

Create src/main/java-templates/com/example/BuildInfo.java:

package com.example;

public final class BuildInfo {
    public static final String VERSION = "${project.version}";

    private BuildInfo() {
    }
}

Execution:

mvn generate-sources

The plugin writes the results to target/generated-sources/com/example/BuildInfo.java and adds the output directory to the Maven main source path. Execution of mvn compile will also go through this generation phase first.

Goal and catalog

Goaldefault stageEnter directoryOutput directory
generate-sourcesgenerate-sourcessrc/main/java-templatestarget/generated-sources
generate-test-sourcesgenerate-test-sourcessrc/test/java-templatestarget/generated-test-sources

The output directories are registered via addCompileSourceRoot and addTestCompileSourceRoot respectively. No file will be generated if the input directory does not exist.

Configuration parameters

parametersDefault valuePurpose
sourceDirectory / outputDirectoryThe main source code directory in the above tableMain source code input and output
testSourceDirectory / testOutputDirectoryTest source code directory in the above tableTest source code input and output
encoding${project.build.sourceEncoding}Template character encoding
delimitersMaven defaults ${...} and @...@Custom placeholder separator
useDefaultDelimiterstrueWhether to keep the default delimiter when configuring a custom delimiter
escapeStringnot setPrevent specific expressions from being interpolated, specified by maven.resources.escapeString
overwritefalseWhether to overwrite unchanged files
skipPomstrueWhether to skip the pom packaging project

The parameters are written in <configuration> of the plugin. After modifying the input directory, check the package declaration and directory structure of the file at the same time.

Source code basis

GenerateSourcesMojo · GenerateTestSourcesMojo · Shared filtering implementation

Architecture diagram