I found this answer about how to do it with Groovy:
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
Is there a better way?
I found this answer about how to do it with Groovy:
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
Is there a better way?
8条答案
按热度按时间2jcobegt1#
实际上,我查看了Gradle项目,它看起来更简洁,因为它使用了Ant's现有结构:
我在下面的Gradle分支中找到了这个,它似乎工作得很好。gradle/gradle-core/branches/RB-0.3/build.gradle
falq053o2#
Mid 2020 Update: Still incubating:
Early 2019 Update:
current()
removed.org.gradle.nativeplatform.platform.OperatingSystem.getDisplayName()
org.gradle.nativeplatform.platform.OperatingSystem.isLinux()
Keep in mind that it's still incubating though.
Mid 2018 Update: just like it was mentioned in comments, now this class moved to a different package, so one should use
org.gradle.nativeplatform.platform.OperatingSystem.current()
As of mid 2015, Peter Kahn's answer is still valid. Environment-based profile activation is still something done relatively easier in Maven. But keep in mind that
org.apache.tools.ant.taskdefs.condition.Os.isFamily
is not exclusive in the sense that if it returns true with one particular parameter it is not necessarily means that it returns false for any other parameter. For instance:It will return true both for
Os.FAMILY_MAC
andOs.FAMILY_UNIX
on MacOS. Usually it is not something you need in build scripts.There is though another way to achieve this using Gradle 2+ API, namely:
Check out the documentation for the org.gradle.nativeplatform.platform.OperatingSystem interface. It is worth to mention that this interface is marked with incubating annotation, that is, "the feature is currently a work-in-progress and may change at any time". The "internal" namespace in the implementation also gives us a hint that we should use this knowing that this can change.
But personally I'd go with this solution. It's just that it's better to write a wrapper class so as not to mess up in case something will change in the future.
tktrz96b3#
人们可以区分Linux、Unix、Windows和OS X之间的构建环境-而Gradle nativeplatform.platform.OperatingSystem则区分目标环境(包括FreeBSD和Solaris)。
也可以使用Ant任务(source):
bweufnob4#
Or you can define
osName
as a string...... and use it later - to include a native library for example:
But it wouldn't change anything since OperatingSystem works exactly like your code:
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 the architecture:
and:
Just be aware that
getArch()
will return:getArch()
will return "x86" on Solaris or "i386" for any other platform.*Edit 2:
Or if you want to avoid any import, you can simply do it yourself:
mbzjlibv5#
Gradle不提供用于检测操作系统的公共API,因此
os.
系统属性是您的最佳选择。2nbm6dog6#
我不喜欢通过属性或Ant任务在Gradle中检测操作系统,并且
OperatingSystem
类不再包含current()
方法。因此,在我看来,检测操作系统的最干净的方法是:
导入默认本机平台:
然后在任务中使用
DefaultNativePlatform
:请注意,这种方法并不理想,因为它使用的是Gradle内部API。
使用Gradle 4.10对其进行了测试。
jvidinwx7#
在没有任何导入的情况下,我从
System
类中获得了这些值,如下所示:kadbb4598#
根据@shabunc的回答,你的一些选择是:
org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
org.apache.tools.ant.taskdefs.condition.Os
org.gradle.nativeplatform.platform.OperatingSystem
org.gradle.internal.os.OperatingSystem
3号和4号基本上是相同的接口,其中
OperatingSystem.current()
实际上是OperatingSystem.isCurrent()
(因为Groovy)。2号是一个可行的选择,但不是优雅的IMHO。
因此,这是你可以如何使用选项1(作为一个例子,没有包括在内):