运行 命令行 Gradle 并保存输出结果
Run a command line with Gradle and Save the output result
我想运行一个命令行Gradle这个命令有一个输出。
我在 windows powershell 中 运行 这个命令:
./mybat.bat myArgs 当我按下回车键时,它会打印一些数字,如下所示:
123456
我想用 gradle 运行 这个命令并保存这个结果 (123456)
这是我在 android build.gradle 文件中编写的一些代码:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', './mybat.bat' , 'foo'//this is myArgs for example
}
这有效并打印值 123456,但我想将它保存在一个变量中,我该怎么做?
正如你在官方文档中看到的那样HERE
这可以通过以下任务来实现
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'mybat.bat', '>', 'log.txt'
doLast {
println "Executed!"
}
}
这将发送 mybat.bat
执行的输出并将结果设置到名为 log 的 txt 文件中。
.
是您拥有脚本的目录。
在我的例子中它是一个项目根目录。
我发现的最佳方法是将 '/c' 添加到命令行参数并使用 standardOutput,这里有一些代码可能会有所帮助其他人:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', '/c', './mybat.bat' , 'foo'//this is myArgs for example
standardOutput = new ByteArrayOutputStream()
doLast {
def result = standardOutput.toString()
println "the result value is: $result"
}
}
我想运行一个命令行Gradle这个命令有一个输出。
我在 windows powershell 中 运行 这个命令:
./mybat.bat myArgs 当我按下回车键时,它会打印一些数字,如下所示:
123456
我想用 gradle 运行 这个命令并保存这个结果 (123456)
这是我在 android build.gradle 文件中编写的一些代码:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', './mybat.bat' , 'foo'//this is myArgs for example
}
这有效并打印值 123456,但我想将它保存在一个变量中,我该怎么做?
正如你在官方文档中看到的那样HERE
这可以通过以下任务来实现
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'mybat.bat', '>', 'log.txt'
doLast {
println "Executed!"
}
}
这将发送 mybat.bat
执行的输出并将结果设置到名为 log 的 txt 文件中。
.
是您拥有脚本的目录。
在我的例子中它是一个项目根目录。
我发现的最佳方法是将 '/c' 添加到命令行参数并使用 standardOutput,这里有一些代码可能会有所帮助其他人:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', '/c', './mybat.bat' , 'foo'//this is myArgs for example
standardOutput = new ByteArrayOutputStream()
doLast {
def result = standardOutput.toString()
println "the result value is: $result"
}
}