Skip to content

Instantly share code, notes, and snippets.

@Singhak
Last active April 10, 2023 02:18
Show Gist options
  • Save Singhak/e30ee4f0db7aa67ae65d0a05beeb2c54 to your computer and use it in GitHub Desktop.
Save Singhak/e30ee4f0db7aa67ae65d0a05beeb2c54 to your computer and use it in GitHub Desktop.
Example of gradle task exec
// First way
task SvnDiif {
new ByteArrayOutputStream().withStream { os ->
exec {
executable = 'svn'
args = ['diff', '-r', "reviosionNo_from:revisionNo_to", "svn_repo", '--summarize']
standardOutput = os
}
def outputAsString = os.toString()
println(outputAsString)
}
//Second way
task RunBatfile {
exec {
workingDir "D:/Bat"
commandLine 'cmd', '/c', '<your_bat_file_name>.bat'
}
}
//Third way
task stopTomcat(type:Exec) {
workingDir '../tomcat/bin'
//on windows:
commandLine 'cmd', '/c', 'stop.bat'
//on linux
commandLine './stop.sh'
}
// Fourth way with error handling
def checkExecResult(execResult, failText, standardOutput){
if (execResult) {
if (execResult.getExitValue() != 0) {
throw new GradleException('Non-zero exit value: ' + execResult.getExitValue())
}
if (standardOutput.toString().contains(failText)){
throw new GradleException('"' + failText + '" string in output: ' + standardOutput.toString())
}
} else {
throw new GradleException('Returned a null execResult object')
}
}
task runApp(dependsOn:':installDebug', type: Exec) {
def sdkPath = android.getSdkDirectory().getAbsolutePath()
def adbPath = sdkPath + '\\platform-tools\\adb.exe'
def activityToRun = '"com.example.myapp/.activities.StartActivity"'
standardOutput = new ByteArrayOutputStream();
commandLine adbPath, 'shell', 'am', 'start', activityToRun
doLast {
checkExecResult(execResult, 'Error', standardOutput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment