I found this answer about how to do it with Groovy:
Detecting the platform (Window or Linux) by Groovy/Grails:
One can differentiate the build environment in between Linux, Unix, Windows and OS X - while the Gradle nativeplatform.platform.OperatingSystem differentiates the target environment (incl. FreeBSD and Solaris).
import org.gradle.internal.os.OperatingSystem
String osName = OperatingSystem.current().getName();
String osVersion = OperatingSystem.current().getVersion();
println "*** $osName $osVersion was detected."
if (OperatingSystem.current().isLinux()) {
// Consider Linux.
} else if (OperatingSystem.current().isUnix()) {
// Consider UNIX.
} else if (OperatingSystem.current().isWindows()) {
// Consider Windows.
} else if (OperatingSystem.current().isMacOsX()) {
// Consider OS X.
} else {
// Unknown OS.
}
One can also use an Ant task (source):
import org.apache.tools.ant.taskdefs.condition.Os
task checkWin() << {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// Consider Windows.
}
}