How to determine current OS from gradle

I found this answer on how to do this with groovy:

Platform discovery (Window or Linux) using groovy / grails :

if (System.properties['os.name'].toLowerCase().contains('windows')) { println "it Windows" } else { println "it not Windows" } 

Is there a better way?

+79
gradle
Jun 27 '12 at 22:22
source share
6 answers

Actually, I looked at the gradle project and it looks a little cleaner as it uses ant existing structure

 import org.apache.tools.ant.taskdefs.condition.Os task checkWin() << { if (Os.isFamily(Os.FAMILY_WINDOWS)) { println "*** WINDOWS " } } 

I found this in the next gradle branch and it seems to work fine gradle / gradle-core / branches / RB-0.3 / build.gradle

+106
Oct 08
source share

Early update'2019 : current() removed.

org.gradle.nativeplatform.platform.OperatingSystem.getDisplayName()

org.gradle.nativeplatform.platform.OperatingSystem.isLinux()

Keep in mind that this still incubates though.

Mid 2011 update : just as it was mentioned in the comments, now this class has been moved to another package, so you should use org.gradle.nativeplatform.platform.OperatingSystem.current()




As of the summer of 2015, Peter Kahn’s response remains valid. Activating an environment-based profile in maven is still relatively easy. But keep in mind that org.apache.tools.ant.taskdefs.condition.Os.isFamily not exclusive in the sense that if it returns true with one specific parameter, this does not necessarily mean that it returns false for any other parameter. For example:

 import org.apache.tools.ant.taskdefs.condition.Os task detect { doLast { println(Os.isFamily(Os.FAMILY_WINDOWS)) println(Os.isFamily(Os.FAMILY_MAC)) println(Os.isFamily(Os.FAMILY_UNIX)) } } 

Os.FAMILY_MAC true for both Os.FAMILY_MAC and Os.FAMILY_UNIX on MacOS. This is usually not what you need in build scripts.

However, there is another way to achieve this using the gradle 2+ API, namely:

 import org.gradle.internal.os.OperatingSystem; task detect { doLast { println(OperatingSystem.current().isMacOsX()) println(OperatingSystem.current().isLinux()) } } 

Check out the document for the org.gradle.nativeplatform.platform.OperatingSystem interface. It is worth noting that this interface is marked with incubation annotation, that is, "this function is currently under development and may change at any time." The "internal" namespace in the implementation also gives us a hint that we should know this, knowing that this can change.

But personally, I would go with this decision. It’s just better to write a wrapper class so you don’t get confused if something changes in the future.

+54
Jul 16 '15 at 1:30
source share

You can distinguish between the build environment between Linux, Unix, Windows, and OSX, and Gradle nativeplatform.platform.OperatingSystem differs in the target environment (including FreeBSD and Solaris).

 String osName = org.gradle.internal.os.OperatingSystem.current().getName(); String osVersion = org.gradle.internal.os.OperatingSystem.current().getVersion(); println "*** $osName $osVersion was detected." if (org.gradle.internal.os.OperatingSystem.current().isLinux()) { // consider Linux. } else if (org.gradle.internal.os.OperatingSystem.current().isUnix()) { // consider UNIX. } else if (org.gradle.internal.os.OperatingSystem.current().isWindows()) { // consider Windows. } else if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) { // consider OSX. } else { // unknown OS. } 
+13
Sep 20 '16 at 11:27
source share

Gradle does not provide a public API for operating system discovery. Hence your best os. properties os. is your best choice.

+6
Jun 29 2018-12-12T00:
source share

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); } 
+2
Mar 15 '18 at 20:48
source share

I don’t like detecting the OS in Gradle through the Ant properties or task, and the OperatingSystem class no longer contains the current() method.

So, in my opinion, the cleanest way to detect the OS would be:

Import DefaultNativePlatform.

 import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform 

Then use DefaultNativePlatform in your task.

 if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows()) { println 'Windows' } 

Keep in mind that this method is not ideal, as it uses the internal Gradle API. Tested with Gradle 4.10. I will update Gradle 5 later.

0
May 13 '19 at 3:33
source share



All Articles