This file contains 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
ant.condition(property: "os", value: "windows") { os(family: "windows") } | |
ant.condition(property: "os", value: "unix" ) { os(family: "unix") } | |
task execCommandLine(type:Exec) { | |
switch(ant.properties.os){ | |
case 'windows': | |
commandLine 'cmd', '/c', 'echo', 'hello' | |
break | |
case 'unix': | |
commandLine 'echo', 'hello' | |
break | |
} | |
standardOutput = new ByteArrayOutputStream() | |
} | |
execCommandLine.doFirst { | |
println 'doFirst' | |
print standardOutput.toString() | |
} | |
execCommandLine << { | |
println 'doLast' | |
print standardOutput.toString() | |
} |
標準出力をByteArrayOutputStreamに割り当て(13行目)、コマンド実行後にそのByteArrayOutputStreamから文字列を取得している(23行目)。
上記のタスクを実行した結果は以下の通り。
当たり前だが、コマンド実行時に出力される文字列は、コマンドの実行後でないと取得できない。$ gradle -q execCommandLine doFirst doLast hello