Fork me on GitHub

Installation and Usage

The Jinja Maven Plugin attempts to render concrete resources using Jinja templating engine during project build time.

Users define template file(s) and corresponding value file(s). When the plugin executes, it substitutes the values from value file(s) in the template file(s) and renders concrete resource(s) at the configured location. This bundle of template file location, value file location(s) and output file location is together called a resource. Users may define one or more resources as part of a resourceSet.

Details of plugin configuration (resource and resourceSet) are available at this link.

Installation

Include the below mentioned pluginRepository in your Maven project to get the latest snapshot of the plugin (based on defined plugin version):

    ...

    <pluginRepositories>
        ...

        <pluginRepository>
            <id>ossrh</id>
            <name>Central Repository OSSRH - Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </pluginRepository>

        ...
    </pluginRepositories>

    ...

To get the releases instead, include the pluginRepository below:

    ...

    <pluginRepositories>
        ...

        <pluginRepository>
             <id>ossrh</id>
             <name>Central Repository OSSRH - Releases</name>
             <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </pluginRepository>

        ...
    </pluginRepositories>

    ...

Once the repository is included add the below plugin to your project:

    ...

    <plugins>
        ...

        <plugin>
            <groupId>com.github.chitralverma</groupId>
            <artifactId>jinja-maven-plugin</artifactId>
            <version>${latest.plugin.version}</version>
            <configuration>
                <!-- Optional configuration to skip the entire goal. Default: false -->
                <skip>false</skip>

                <!-- Optional configuration to fail if values for template are missing. Default: true -->
                <failOnMissingValues>true</failOnMissingValues>

                <!-- Optional configuration to control if output files can be overwritten. Default: false -->
                <overwriteOutput>false</overwriteOutput>

                <!-- Required configuration for resource set.
                A resource set is bundle of one or more resources which can be translated to a rendering job.
                It contains a template file path, one or more value files and an output file path.
                -->
                <resourceSet>
                    <resource>
                        <templateFilePath>/path/to/template/a_template.j2</templateFilePath>
                        <valueFiles>
                            <param>/path/to/values/value_1.json</param>
                            <param>/path/to/values/value_2.json</param>
                        </valueFiles>
                        <outputFilePath>src/main/resources/jinja/results/config.json</outputFilePath>
                    </resource>
                </resourceSet>
            </configuration>
        </plugin>

        ...
    </plugins>

    ...

Usage

Requirements

This project has the following requirements,

  • JDK 8
  • Maven 3.3 and above

Manually rendering resources using the command-line

Once the plugin is installed successfully and configured in pom.xml, it can be executed via command-line.

To run the Jinja Maven Plugin, execute the command below:

  mvn jinja:generate

where jinja refers to the plugin's alias, and generate refers to the plugin goal.

The plugin can also be executed with other phases/goals like:

  mvn clean jinja:generate

Automatically rendering resources at build time

To automatically execute the plugin at build time, add an execution to the plugin:

    ...

    <plugins>
        ...

        <plugin>
            <groupId>com.github.chitralverma</groupId>
            <artifactId>jinja-maven-plugin</artifactId>
            <version>${latest.plugin.version}</version>
            <configuration>
                ...
            </configuration>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        ...
    </plugins>

    ...

Once the above execution is added, the configured resources will rendered at build time automatically. For example when compile, package, and other phases are executed:

  mvn clean package

Note: Ensure that the plugin is configured with <skip>false</skip>, otherwise the execution will be skipped.

Build this plugin from source

To build this project from source, run the commands below,

  # Clone project to current working directory
  git clone https://github.com/chitralverma/jinja-maven-plugin.git
  # Clone project to current working directory
  git clone https://github.com/chitralverma/jinja-maven-plugin.git
  # Change directory to cloned project root
  cd jinja-maven-plugin
  # Build project
  mvn clean install