Created
August 22, 2016 09:13
-
-
Save amaksoft/b413b70732f9290a15d0a8de0cd5119e to your computer and use it in GitHub Desktop.
An extremely simple plugin for exporting data from gradle script into properties file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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