Groovyでそれを行う方法について、この回答を見つけました:
Groovy/Grails によるプラットフォーム (Windows または Linux) の検出:
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
より良い方法はありますか?
Groovyでそれを行う方法について、この回答を見つけました:
Groovy/Grails によるプラットフォーム (Windows または Linux) の検出:
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
より良い方法はありますか?
実際、私は Gradle プロジェクトを調べましたが、これはAnt の既存の構造を使用しているため、少しきれいに見えます。
import org.apache.tools.ant.taskdefs.condition.Os
task checkWin() << {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
println "*** Windows "
}
}
これは次の Gradle ブランチで見つけましたが、うまく機能しているようです。gradle/gradle-core/branches/RB-0.3/build.gradle
Linux、Unix、Windows、および OS X の間でビルド環境を区別できますが、Gradle nativeplatform.platform.OperatingSystemはターゲット環境 ( FreeBSDおよびSolarisを含む) を区別します。
import org.gradle.internal.os.OperatingSystem
String osName = OperatingSystem.current().getName();
String osVersion = OperatingSystem.current().getVersion();
println "*** $osName $osVersion was detected."
if (OperatingSystem.current().isLinux()) {
// Consider Linux.
} else if (OperatingSystem.current().isUnix()) {
// Consider UNIX.
} else if (OperatingSystem.current().isWindows()) {
// Consider Windows.
} else if (OperatingSystem.current().isMacOsX()) {
// Consider OS X.
} else {
// Unknown OS.
}
Ant タスク ( source )を使用することもできます。
import org.apache.tools.ant.taskdefs.condition.Os
task checkWin() << {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// Consider Windows.
}
}
Gradle は、オペレーティング システムを検出するための公開 API を提供していません。したがって、os.
システム プロパティが最善の策です。