Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An extremely simple plugin for exporting data from gradle script into properties file.
/**
* Created by amak on 8/19/16.
*
* An extremely simple plugin for exporting data from gradle script into properties file.
* It's very useful for CI systems: turn your gradle script variables into environment variables.
*
* Plugin adds an extension exportExt for variables you want to export:
*
* exportExt {
* prop "version", myProjectVersion
* prop "projectName", "myProjectName"
* prop "NoDeploy", true
* }
*
* Data is exported by calling exportProps task. Default filename is "build/build.properties" in your root project
* You can specify different filename with command line argument -PexportFile="path/to/your/file"
*
* Example bash script:
*
* #!/bin/bash
*
* PROPS_FILE = "path/to/your/file"
* ./gradlew exportProps -PexportFile=$PROPS_FILE #Exporting properties
*
* source $PROPS_FILE #importing variables
*
* mkdir /my/deploy/path/$version
*
* #End of bash script
*
*/
class ExportDataPlugin implements Plugin<Project> {
@Override
void apply(Project target) {
target.extensions.create(ExportExtension.NAME, ExportExtension)
target.tasks.create(ExportPropsTask.NAME, ExportPropsTask)
}
}
class ExportExtension {
static String NAME = "exportExt"
Properties props = new Properties()
def prop(String key, String value) {
this.props.setProperty(key, value)
}
}
class ExportPropsTask extends DefaultTask {
static NAME = "exportProps"
ExportExtension extension
ExportPropsTask() {
project.afterEvaluate {
extension = project.extensions."${ExportExtension.NAME}"
}
}
@TaskAction
void exec() {
File file = project.hasProperty('exportFile') ? new File(project.exportFile) : new File(project.rootProject.buildDir, "build.properties")
println "Creating file ${file.absolutePath} with project info..."
file.getParentFile().mkdirs()
def writer = new FileWriter(file)
extension.props.store(writer, "Build info")
}
}
apply plugin:ExportDataPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment