Or you can define osName as a string ...
import org.gradle.internal.os.OperatingSystem switch (OperatingSystem.current()) { case OperatingSystem.LINUX: project.ext.osName = "linux"; break ; case OperatingSystem.MAC_OS: project.ext.osName = "macos"; break ; case OperatingSystem.WINDOWS: project.ext.osName = "windows"; break ; }
... and use it later - to include a native library, for example:
run { systemProperty "java.library.path", "lib/$osName" }
But that will not change anything, since OperatingSystem works just like your code:
public static OperatingSystem forName(String os) { String osName = os.toLowerCase(); if (osName.contains("windows")) { return WINDOWS; } else if (osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx")) { return MAC_OS; } else if (osName.contains("sunos") || osName.contains("solaris")) { return SOLARIS; } else if (osName.contains("linux")) { return LINUX; } else if (osName.contains("freebsd")) { return FREE_BSD; } else { // Not strictly true return UNIX; } }
source: https://github.com/gradle/gradle/blob/master/subprojects/base-services/src/main/java/org/gradle/internal/os/OperatingSystem.java
Edit:
You can do the same for Arch:
project.ext.osArch = OperatingSystem.current().getArch(); if ("x86".equals(project.ext.osArch)) { project.ext.osArch = "i386"; }
as well as:
run { systemProperty "java.library.path", "lib/$osName/$osArch" }
Just know that getArch () will return:
- PDA on PowerPC
- "amd64" on 64b
- "i386" OR "x86" on 32b.
getArch () will return "x86" to Solaris or "i386" for any other platform.
Edit2:
Or, if you want to avoid importing, you can just do it yourself:
def getOsName(project) { final String osName = System.getProperty("os.name").toLowerCase(); if (osName.contains("linux")) { return ("linux"); } else if (osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx")) { return ("macos"); } else if (osName.contains("windows")) { return ("windows"); } else if (osName.contains("sunos") || osName.contains("solaris")) { return ("solaris"); } else if (osName.contains("freebsd")) { return ("freebsd"); } return ("unix"); } def getOsArch(project) { final String osArch = System.getProperty("os.arch"); if ("x86".equals(osArch)) { return ("i386"); } else if ("x86_64".equals(osArch)) { return ("amd64"); } else if ("powerpc".equals(osArch)) { return ("ppc"); } return (osArch); }
Tirz Mar 15 '18 at 20:48 2018-03-15 20:48
source share