我找到了关于如何用groovy做到这一点的答案:
通过groovy/grails检测平台(Window或Linux):
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
Pet*_*ahn 110
实际上,我看了gradle项目,这看起来有点干净,因为它使用了ant的现有结构
import org.apache.tools.ant.taskdefs.condition.Os
task checkWin() << {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
println "*** WINDOWS "
}
}
Run Code Online (Sandbox Code Playgroud)
我在下面的gradle分支中找到了它,它似乎很好地运行gradle/gradle-core/branches/RB-0.3/build.gradle
sha*_*unc 59
2019年初更新:current()
删除.
org.gradle.nativeplatform.platform.OperatingSystem.getDisplayName()
org.gradle.nativeplatform.platform.OperatingSystem.isLinux()
请记住,它仍然在孵化.
2018年中期更新:就像在评论中提到的那样,现在这个类移到了另一个包,所以应该使用org.gradle.nativeplatform.platform.OperatingSystem.current()
截至2015年夏天,Peter Kahn的回答仍然有效.基于环境的配置文件激活仍然在maven中相对容易完成.但请记住,这org.apache.tools.ant.taskdefs.condition.Os.isFamily
并不是唯一的意义,如果它返回true与一个特定的参数,它不是必然意味着它返回任何其他参数的假.例如:
import org.apache.tools.ant.taskdefs.condition.Os
task detect {
doLast {
println(Os.isFamily(Os.FAMILY_WINDOWS))
println(Os.isFamily(Os.FAMILY_MAC))
println(Os.isFamily(Os.FAMILY_UNIX))
}
}
Run Code Online (Sandbox Code Playgroud)
对于MacOS Os.FAMILY_MAC
和Os.FAMILY_UNIX
MacOS 都将返回true .通常,它不是构建脚本中需要的东西.
使用gradle 2+ API可以实现另一种方法,即:
import org.gradle.internal.os.OperatingSystem;
task detect {
doLast {
println(OperatingSystem.current().isMacOsX())
println(OperatingSystem.current().isLinux())
}
}
Run Code Online (Sandbox Code Playgroud)
查看org.gradle.nativeplatform.platform.OperatingSystem接口的doc .值得一提的是,此界面标有孵化注释,即"该功能目前正在进行中,可能随时发生变化".实现中的"内部"命名空间也给了我们一个暗示,我们应该知道这可以改变.
但就个人而言,我会选择这个解决方案.只是最好编写一个包装类,以防万一将来会发生变化.
Mar*_*ler 15
可以区分Linux,Unix,Windows和OSX之间的构建环境 - 而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 OSX.
} else {
// unknown OS.
}
Run Code Online (Sandbox Code Playgroud)
或者您可以将 osName 定义为字符串...
import org.gradle.internal.os.OperatingSystem
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
project.ext.osName = "Linux";
break;
case OperatingSystem.MAC_OS:
project.ext.osName = "macOS";
break;
case OperatingSystem.WINDOWS:
project.ext.osName = "Windows";
break;
}
Run Code Online (Sandbox Code Playgroud)
...并稍后使用它 - 包括一个本地库,例如:
run {
systemProperty "java.library.path", "lib/$osName"
}
Run Code Online (Sandbox Code Playgroud)
但它不会改变任何东西,因为操作系统的工作方式与您的代码完全一样:
public static OperatingSystem forName(String os) {
String osName = os.toLowerCase();
if (osName.contains("Windows")) {
return WINDOWS;
} else if (osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx")) {
return MAC_OS;
} else if (osName.contains("sunos") || osName.contains("solaris")) {
return SOLARIS;
} else if (osName.contains("linux")) {
return LINUX;
} else if (osName.contains("freebsd")) {
return FREE_BSD;
} else {
// Not strictly true
return UNIX;
}
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/gradle/gradle/blob/master/subprojects/base-services/src/main/java/org/gradle/internal/os/OperatingSystem.java
编辑:
您可以对架构执行相同的操作:
project.ext.osArch = OperatingSystem.current().getArch();
if ("x86".equals(project.ext.osArch)) {
project.ext.osArch = "i386";
}
Run Code Online (Sandbox Code Playgroud)
和:
run {
systemProperty "java.library.path", "lib/$osName/$osArch"
}
Run Code Online (Sandbox Code Playgroud)
请注意 getArch() 将返回:
getArch() 将在 Solaris 上返回“x86”或在任何其他平台上返回“i386”。
编辑2:
或者,如果您想避免任何导入,您可以简单地自己做:
def getOsName(project) {
final String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("linux")) {
return ("linux");
} else if (osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx")) {
return ("macos");
} else if (osName.contains("windows")) {
return ("windows");
} else if (osName.contains("sunos") || osName.contains("solaris")) {
return ("solaris");
} else if (osName.contains("freebsd")) {
return ("freebsd");
}
return ("unix");
}
def getOsArch(project) {
final String osArch = System.getProperty("os.arch");
if ("x86".equals(osArch)) {
return ("i386");
}
else if ("x86_64".equals(osArch)) {
return ("amd64");
}
else if ("powerpc".equals(osArch)) {
return ("ppc");
}
return (osArch);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我不喜欢通过属性或 Ant 任务检测 Gradle 中的操作系统,并且OperatingSystem
该类不再包含该current()
方法。
所以,在我看来,检测操作系统的最干净的方法是:
导入 DefaultNativePlatform:
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
Run Code Online (Sandbox Code Playgroud)
然后DefaultNativePlatform
在您的任务中使用:
if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows()) {
println 'Windows'
}
Run Code Online (Sandbox Code Playgroud)
请注意,此方法并不理想,因为它使用 Gradle 内部 API。
它使用 Gradle 4.10 进行了测试。
归档时间: |
|
查看次数: |
39546 次 |
最近记录: |