Home

Awesome

<p align="left"> <a href="https://github.com/etiennestuder/gradle-rocker-plugin/actions?query=workflow%3A%22Build+Gradle+project%22"><img src="https://github.com/etiennestuder/gradle-rocker-plugin/workflows/Build%20Gradle%20project/badge.svg"></a> </p>

gradle-rocker-plugin

The work on this software project is in no way associated with my employer nor with the role I'm having at my employer. Any requests for changes will be decided upon exclusively by myself based on my personal preferences. I maintain this project as much or as little as my spare time permits.

Overview

Gradle plugin that integrates the Rocker template engine.

For each named Rocker configuration declared in the build, the plugin adds a task to generate the Java sources from the specified Rocker templates and includes the generated Java sources in the matching source set, if existing. The code generation tasks participate in task configuration avoidance, in build configuration caching, in task output caching, and in incremental builds. Additionally, the compile task itself is incremental, meaning it is optimized so that only templates which have changed are regenerated. The plugin can be applied on both Java projects and Android projects.

You can find more details about the actual Rocker source code generation in the Rocker documentation.

The Rocker plugin is hosted at the Gradle Plugin Portal.

Build scan

Recent build scan: https://scans.gradle.com/s/nrhggndytzjze

Find out more about build scans for Gradle and Maven at https://scans.gradle.com.

Functionality

The following functionality is provided by the Rocker plugin:

The following Gradle configuration changes are contributed by the Rocker plugin:

The following Gradle features are supported by the Rocker plugin:

Compatibility

Plugin versionCompatible Gradle versionsSupport for Gradle Kotlin DSLSupport for Gradle Configuration Cache
3.0+6.1+YesYes
2.0+6.0+YesYes
1.0.15.0+, 6.0+YesNo

Configuration

Apply Rocker plugin

Apply the nu.studer.rocker plugin to your Gradle project.

plugins {
    id 'nu.studer.rocker' version '3.0.4'
}

Please refer to the Gradle DSL PluginDependenciesSpec to understand the behavior and limitations when using the new syntax to declare plugin dependencies.

Define Rocker configurations

This is a sample configuration:

plugins {
    id 'nu.studer.rocker' version '3.0.4'
    id 'java'
}

repositories {
    jcenter()
}

rocker {
    version = '1.3.0'  // optional
    configurations{
        main {
            templateDir = file('src/rocker')
            outputDir = file('src/generated/rocker')
            optimize = true  // optional
        }
    }
}

The rocker main configuration declares that the Rocker templates are in src/rocker and the generated Java sources need to end up in src/generated/rocker. It further declares via the optimize property that the generated Java sources should be optimized to not contain any code that allows for hot reload via Rocker. Since the name of the configuration is main, the generated sources are added to the main source set contributed by the applied java plugin.

Given the configuration above, you can invoke the Rocker template engine by issuing ./gradlew compileRocker. You can also directly call ./gradlew compileJava which first generates the Java sources from the Rocker templates, and then compiles these Java sources as part of compiling all sources in the main source set.

Since we declared to use version 1.3.0 of the Rocker template engine, all Rocker dependencies of all Gradle configurations will be of that given version.

I suggest you use the Continuous build feature of Gradle instead of using the Rocker hot reload feature. Declare optimize = true in the Rocker configuration of your Gradle build, and then run your build with the -t command line option. In addition, deactivating the hot reload feature of Rocker will enable the Rocker tasks for task output caching by the Gradle Build Cache.

Complete Rocker configuration options

For each named configuration, the following options can be configured:

Warning: do not configure any of templateDir, outputDir, and classDir to point to the same directory or to a directory that also contains other content.

Invocation

Invoke Rocker task

You can generate the Java sources for a given Rocker configuration by invoking the command compile<configName>Rocker, e.g. compileTestRocker. The only exception being main that is abbreviated to compileRocker, similarly to how it is done for the JavaCompile tasks contributed by the java plugin.

Examples

See the self-contained example build scripts for the Groovy DSL and the Kotlin DSL.

Changelog

Feedback and Contributions

Both feedback and contributions are very welcome.

Acknowledgements

License

This plugin is available under the Apache License, Version 2.0.

(c) by Etienne Studer