gradle から bash scriptを起動させたい時 exec{} と .execute() の違い
gradleからscriptを起動させる際に、方法は幾つかありますが、
exec{} と .execute() の違いについてメモります。
exec{} :
tasks.withType(Test) { exec { commandLine '/bin/sh', '-c', "./test.sh" } }
.execute() :
tasks.withType(Test) { "./test.sh".execute() }
どちらからでもスクリプトは起動できますが、
一番目のexec{} では、スクリプトが起動起動して、それが実行し終わるまで待ってくれます。
更に標準出力としてgradleのコンソールに出力が出ます。
対して二番目の .execute()の方法は、
スクリプトを実行させたらすぐに次の行に移行しgradleのタスクを実行します。
ちなみに標準出力の結果を得たいのであれば
exec{} :
tasks.withType(Test) { def buildNumberStdOut = new ByteArrayOutputStream() exec { commandLine '/bin/sh', '-c', "./test.sh" standardOutput = buildNumberStdOut } println(buildNumberStdOut.toString().trim()) }
で結果を得る事ができます。