Detecting the platform (Window or Linux) by Groovy/Grails
Asked Answered
L

2

32

Is there a way to detect the platform (Window / Linux) in which the website is running by Groovy / Grails?

Lili answered 14/1, 2011 at 8:35 Comment(0)
R
63
System.properties['os.name']

will return the name of the OS, e.g. "Windows XP". So if you want to figure out whether you're running on Windows or not, you could do something like:

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

Alternatively, org.apache.commons.lang.SystemUtils (from the Apache commons-lang project) exposes some boolean constants that provide the same information as the code above, e.g.

SystemUtils.IS_OS_MAC
SystemUtils.IS_OS_WINDOWS
SystemUtils.IS_OS_UNIX

More specific constants such as these are also available

SystemUtils.IS_OS_WINDOWS_2000
SystemUtils.IS_OS_SOLARIS
SystemUtils.IS_OS_MAC_OSX
Ratib answered 14/1, 2011 at 8:47 Comment(1)
And in order to get the OS of a Jenkins build node (this is what I was looking for) System.properties won't help you since it contains the properties of the Jenkins instance. Instead you can use onWindows = (env['OS'] ?: "").toLowerCase().contains('windows');Biogen
T
3

Or for short:

if (System.env['OS'].contains('Windows')){ println "it's Windows" }

Since Groovy provides a map access to getAt/putAt methods.

Transaction answered 12/5, 2018 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.