Skip to content

Instantly share code, notes, and snippets.

@8enet
Last active September 14, 2016 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8enet/4af5c10e7a3137ba07c7c7008323a819 to your computer and use it in GitHub Desktop.
Save 8enet/4af5c10e7a3137ba07c7c7008323a819 to your computer and use it in GitHub Desktop.
自动保存配置到properties文件中,比如对应版本号的递增,实现每次构建后版本不同
apply plugin:MySavePlugin
class MySavePlugin implements Plugin<Project>{
@Override
void apply(Project project) {
def file=project.rootProject.file("myprop.properties")
project.extensions.create("myprop",InnerStorage,file)
}
}
class InnerStorage {
Properties myProp
File pFile;
InnerStorage(File file){
pFile=file;
loadProperty()
}
private loadProperty(){
myProp = new Properties()
if(!pFile.exists()){
pFile.createNewFile()
}else {
pFile.withInputStream {
ins -> myProp.load(ins)
}
}
println("loadProperty from $pFile --> $myProp")
}
def propertyMissing(String name, value) {
myProp.setProperty(name,String.valueOf(value))
save()
}
def propertyMissing(String name) {
def v=myProp.getProperty(name)
v.integer?v.toInteger():v.toString()
}
private save(){
if(!pFile.exists()){
pFile.createNewFile()
}
pFile.withOutputStream {
os -> myProp.store(os,null)
}
}
}

在其他build.gradle 中

apply from: 'mysave.gradle'

task testSave(){
	println(project.myprop.abc)

    project.myprop.abc2++

    println(project.myprop.abc2)

    project.myprop.abc3="lldof"
    project.myprop.abc=234234
}

每次修改即可自动保存到myprop.properties文件中

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment