通过groovy/grails检测平台(Window或Linux)

Hoà*_*ong 30 grails groovy platform-detection

有没有办法检测Groovy/Grails运行网站的平台(Window/Linux)?

Dón*_*nal 56

System.properties['os.name']
Run Code Online (Sandbox Code Playgroud)

将返回操作系统的名称,例如"Windows XP".因此,如果您想知道自己是否在Windows上运行,可以执行以下操作:

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

或者,org.apache.commons.lang.SystemUtils(来自Apache commons-lang项目)公开一些布尔常量,它们提供与上面代码相​​同的信息,例如

SystemUtils.IS_OS_MAC
SystemUtils.IS_OS_WINDOWS
SystemUtils.IS_OS_UNIX
Run Code Online (Sandbox Code Playgroud)

还可以使用更具体的常量

SystemUtils.IS_OS_WINDOWS_2000
SystemUtils.IS_OS_SOLARIS
SystemUtils.IS_OS_MAC_OSX
Run Code Online (Sandbox Code Playgroud)