Is there a way to be able to execute a task on both Windows and Mac if the commands take a different form? For example:
task stopTomcat(type:Exec) {
// use this command line if on Windows
commandLine 'cmd', '/c', 'stop.cmd'
// use the command line if on Mac
commandLine './stop.sh'
}
How would you do this in Gradle?
You can conditionally set the commandLine
property based on the value of a system property.
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine 'cmd', '/c', 'stop.cmd'
} else {
commandLine './stop.sh'
}
If the script or executable were the same on windows and linux then you'd be able to do the following so that you only have to define the arguments once by calling a function like so:
import org.apache.tools.ant.taskdefs.condition.Os
task executeCommand(type: Exec) {
commandLine osAdaptiveCommand('aws', 'ecr', 'get-login', '--no-include-email')
}
private static Iterable<String> osAdaptiveCommand(String... commands) {
def newCommands = []
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
newCommands = ['cmd', '/c']
}
newCommands.addAll(commands)
return newCommands
}