/simple-java-gradle-plugin

Sample project to demonstrate the creation of a java Gradle plugin

Primary LanguageJava

Simple Java Gradle Plugin

Overview

This repository contains an example of a simple Java Gradle plugin. The purpose of this example is to demonstrate:

  • Creation of a Java Gradle plugin
  • Generation of dynamic Gradle tasks
  • Basic file operations
  • Usage of third-party dependencies (JSON Patch)

The main project is nothing more than a template file and a properties file. The environmentTemplate.json file contains some properties describing a typical development environment. The environments.properties file contains key-value properties for each environment that will be injected into the template by the plugin.

The plugin executes the following logic:

  • The plugin reads the key-value pairs in the environments.properties file
  • For every key found, it will generate and configure a Gradle task of type SimpleTask (defined by the plugin)
  • Each SimpleTask will take the environmentTemplate.json file and inject the appropriate properties (this is done via JSON Patch), and it will generate a new file with the newly injected properties:

Example:

Template file

{
  "environmentName": "N/A",
  "description": "N/A",
  "applicationName": "Sample Application"
}

Properties

dev=Contains the latest code. Developer's playground to test integration with other systems.

Generated file

{
  "environmentName" : "dev",
  "description" : "Contains the latest code. Developer's playground to test integration with other systems.",
  "applicationName" : "Sample Application"
}

Usage

List all tasks generated by the SimplePlugin:

$ gradle tasks --all

Dynamic Tasks (type: SimpleTask) tasks
--------------------------------------
generateAllEnvironmentFiles - Generates files for all environments.
generatedevfile - Generates a file for the dev environment
generatelocalfile - Generates a file for the local environment
generateprodfile - Generates a file for the prod environment
generatestagefile - Generates a file for the stage environment

Run one or multiple tasks:

$ gradle generatedevfile

> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:processResources NO-SOURCE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
> Task :generatedevfile

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

Run all tasks via the master task (depends on all generated tasks)

$ gradle generateAllEnvironmentFiles

The output files of all dynamic tasks can be seen in the build/generated directory