Json 编辑后未保存存储库中的本地文件(groovy/jenkins)
在詹金斯的工作中,我查看了一个存储库。在该repo中,有一个文件我想在作业期间编辑。但文件似乎没有保存。我有这样一种方法:Json 编辑后未保存存储库中的本地文件(groovy/jenkins),json,jenkins,groovy,Json,Jenkins,Groovy,在詹金斯的工作中,我查看了一个存储库。在该repo中,有一个文件我想在作业期间编辑。但文件似乎没有保存。我有这样一种方法: def updateFile(id, key){ def inputFile = readFile("${workspace}/config/cnf.json") def inputJSON = new JsonSlurper().parseText(inputFile) inputJSON."${key}"[0].pref = "${id}"
def updateFile(id, key){
def inputFile = readFile("${workspace}/config/cnf.json")
def inputJSON = new JsonSlurper().parseText(inputFile)
inputJSON."${key}"[0].pref = "${id}"
def result = JsonOutput.toJson(inputJSON)
//here it is changed.
println "result:\n${result}"
inputFile << "${JsonOutput.prettyPrint(result)}"
//and now it is again the old one.
println "Hier: \n ${inputFile}"
}
writeFile file:"${workspace}/config/cnf.json", text:result
有没有保存现有文件的好方法 ifreadFile(“${workspace}/config/cnf.json”)
工作正常
然后,要编写文件,请使用以下方法:
def updateFile(id, key){
def inputFile = readFile("${workspace}/config/cnf.json")
def inputJSON = new JsonSlurper().parseText(inputFile)
inputJSON."${key}"[0].pref = "${id}"
def result = JsonOutput.toJson(inputJSON)
//here it is changed.
println "result:\n${result}"
inputFile << "${JsonOutput.prettyPrint(result)}"
//and now it is again the old one.
println "Hier: \n ${inputFile}"
}
writeFile file:"${workspace}/config/cnf.json", text:result
谢谢你的帮助。我测试过了。但是我得到了错误:java.io.NotSerializableException:groovy.json.internal.LazyMap:-(使用
JsonSlurperClassic
而不是JsonSlurper
。您只能在jenkins管道中使用可序列化对象。JsonSlurperClassic返回正常的java.util.HashMap
而不是LazyMap
。