gradle-replacer

Gradle plugin to replace text

License

License

Categories

Categories

Gradle Build Tools
GroupId

GroupId

com.github.ksoichiro
ArtifactId

ArtifactId

gradle-replacer
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

gradle-replacer
Gradle plugin to replace text
Project URL

Project URL

https://github.com/ksoichiro/gradle-replacer
Source Code Management

Source Code Management

https://github.com/ksoichiro/gradle-replacer

Download gradle-replacer

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ksoichiro/gradle-replacer/ -->
<dependency>
    <groupId>com.github.ksoichiro</groupId>
    <artifactId>gradle-replacer</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ksoichiro/gradle-replacer/
implementation 'com.github.ksoichiro:gradle-replacer:0.1.1'
// https://jarcasting.com/artifacts/com.github.ksoichiro/gradle-replacer/
implementation ("com.github.ksoichiro:gradle-replacer:0.1.1")
'com.github.ksoichiro:gradle-replacer:jar:0.1.1'
<dependency org="com.github.ksoichiro" name="gradle-replacer" rev="0.1.1">
  <artifact name="gradle-replacer" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ksoichiro', module='gradle-replacer', version='0.1.1')
)
libraryDependencies += "com.github.ksoichiro" % "gradle-replacer" % "0.1.1"
[com.github.ksoichiro/gradle-replacer "0.1.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

gradle-replacer

Build Status Maven Central Coverage Status License

Gradle plugin that provides a minimalistic template engine feature.

TL;DR

You can maintain any configuration files with "properties" format.
The plugin copies files from templates and replaces tags with properties for each configurations.

src/main/templates/config.xml
src/dev/build.properties
src/production/build.properties
↓
build/outputs/dev/config.xml
build/outputs/production/config.xml

Example

Build script

build.gradle

buildscript {
    repositories {
        maven {
            url uri('https://oss.sonatype.org/content/repositories/snapshots/')
        }
    }
    dependencies {
        classpath 'com.github.ksoichiro:gradle-replacer:0.1.0-SNAPSHOT'
    }
}

apply plugin: 'com.github.ksoichiro.replacer'

replacer {
    configurations {
        dev
        production
    }
}

Inputs

src/main/templates/config.xml

<server>
    <url>@SERVER_URL@</url>
    <port>@SERVER_PORT@</port>
</server>

src/main/build.properties

SERVER_URL=ssl://192.168.100.56
SERVER_PORT=9900

src/production/build.properties

SERVER_URL=ssl://example.com
SERVER_PORT=9901

Execution

$ ./gradlew replacerGenerate

Outputs

build/outputs/dev/config.xml

<server>
    <url>ssl://192.168.100.56</url>
    <port>9900</port>
</server>

build/outputs/production/config.xml

<server>
    <url>ssl://example.com</url>
    <port>9901</port>
</server>

Usage

Tasks

Clean

Deletes build/outputs and build/archives directory.

$ ./gradlew replacerClean

Generate (Copy and replace)

Copies source files and replace tags.

$ ./gradlew replacerGenerate

Archive

Archives the generated sources.
This depends on replacerGenerate task.

$ ./gradlew replacerArchive

Configurations

replacer {
    // Definition of the targets:
    // e.g. develop, staging, production
    // You need to define them at least one.
    configurations {
        // These are treated as closures.
        // You can omit {}.
        dev {
        }
        production
    }

    // Source directory
    srcDir = "src"

    // Base source directory under srcDir
    srcMainDir = "main"

    // Template files directory under srcMainDir
    // or srcDir/<configuration name>/
    templateDir = "templates"

    // Generated files directory under build
    outputDir = "outputs"

    // Archived files directory under build
    archiveDir = "archives"

    // Properties file directory under templateDir
    properties = "build.properties"

    // DateFormat used for directory name under archiveDir
    archiveIdFormat = "yyyyMMddHHmmss"

    // Archive types for "replacerArchive" task.
    // Available type: zip, gzip, bzip2
    archiveType = "zip"

    // Files to be excluded
    excludes = [ ".gitkeep", ".swp" ]
}

Why?

  • I have had to maintain some configuration files for multiple environments. All the config files have same structure and almost all contents are identical. Only some values are different. Therefore, I wanted some tools to manage them with one template file and multiple properties files.
    The tool should be:

    • Easy to set up
    • Easy to maintain configurations

    In my team, most project members have Java environment, so I thought the Gradle plugin is the best for that purpose.

  • Gradle (groovy) has template engines, but they are too functional.
    I wanted more intelligible, simple tool.
    Also, I didn't want the tool to require scripts or complex setting files.
    Just install, write minimum configurations and execute.

License

Copyright (c) 2015 Soichiro Kashima
Licensed under MIT license.
See the bundled LICENSE file for details.

Versions

Version
0.1.1
0.1.0