【发布时间】:2011-06-09 00:32:12
【问题描述】:
有没有办法通过 Groovy / Grails 检测网站运行的平台(Window / Linux)?
【问题讨论】:
标签: grails groovy platform-detection
有没有办法通过 Groovy / Grails 检测网站运行的平台(Window / Linux)?
【问题讨论】:
标签: grails groovy platform-detection
System.properties['os.name']
将返回操作系统的名称,例如“Windows XP”。因此,如果您想确定您是否在 Windows 上运行,您可以执行以下操作:
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
或者,org.apache.commons.lang.SystemUtils(来自Apache commons-lang 项目)公开了一些布尔常量,这些常量提供与上述代码相同的信息,例如
SystemUtils.IS_OS_MAC
SystemUtils.IS_OS_WINDOWS
SystemUtils.IS_OS_UNIX
也可以使用更具体的常量,例如这些
SystemUtils.IS_OS_WINDOWS_2000
SystemUtils.IS_OS_SOLARIS
SystemUtils.IS_OS_MAC_OSX
【讨论】:
或简称:
if (System.env['OS'].contains('Windows')){ println "it's Windows" }
由于 Groovy 提供了对 getAt/putAt methods 的映射访问。
【讨论】: