How do you support a Gradle Exec task for both Mac and PC?
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
}