Gradle执行Shell命令并获取执行结果|进行JSON数据解析
·
一、在gradle中新增自定的task
task custom {
def out = new ByteArrayOutputStream()
def cmd = 'ls -l'
exec {
ExecSpec execSpec ->
executable 'bash'
args '-c', cmd
standardOutput = out
}
println(out.toString())
}
通过调用exec
就可以执行那些可以在Terminal
中运行的命令了,standardOutput
参数就可以获取到命令执行的结果信息,执行结果如下:
二、上面说到可以执行shell命令,当你执行的是个curl
时便有可能会返回JSON
格式的数据这时候就需要进行解析了
- 解析也是很简单,只需要使用到
groovy.json.JsonSlurper()
task parseJson {
def json = '{"json":"哈哈哈"}'
def parsedJson = new groovy.json.JsonSlurper().parseText(json)
println(parsedJson)
//直接.json的key获取值
println(parsedJson.json)
}
执行结果如下:
更多推荐
所有评论(0)