Gradle에서 현재 OS를 감지하는 방법
그루비로 수행하는 방법에 대한 답변을 찾았습니다.
groovy / grails로 플랫폼 (Window 또는 Linux) 감지 :
if (System.properties['os.name'].toLowerCase().contains('windows')) {
println "it's Windows"
} else {
println "it's not Windows"
}
더 좋은 방법이 있습니까?
사실 저는 gradle 프로젝트를 봤는데 개미의 기존 구조를 사용하기 때문에 조금 더 깔끔해 보입니다.
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 작동하는 것 같습니다.
2019 년 초반 업데이트 : current()삭제되었습니다.
org.gradle.nativeplatform.platform.OperatingSystem.getDisplayName()
org.gradle.nativeplatform.platform.OperatingSystem.isLinux()
그래도 여전히 배양 중이라는 것을 명심하십시오 .
2018 년 중반 업데이트 : 댓글에서 언급했듯이 이제이 클래스는 다른 패키지로 옮겨 졌으므로 사용해야합니다.org.gradle.nativeplatform.platform.OperatingSystem.current()
Summer'2015 현재 Peter Kahn의 답변은 여전히 유효합니다. 환경 기반 프로파일 활성화는 여전히 maven에서 비교적 쉽게 수행되는 작업입니다. 그러나 org.apache.tools.ant.taskdefs.condition.Os.isFamily하나의 특정 매개 변수와 함께 true를 반환하는 경우 반드시 다른 매개 변수에 대해 false를 반환한다는 의미는 아니라는 점에서 배타적이지 않습니다. 예를 들면 :
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))
}
}
MacOS Os.FAMILY_MAC와 Os.FAMILY_UNIXMacOS 모두에서 true를 반환합니다 . 일반적으로 빌드 스크립트에 필요한 것은 아닙니다.
gradle 2+ API를 사용하여이를 달성하는 또 다른 방법이 있습니다.
import org.gradle.internal.os.OperatingSystem;
task detect {
doLast {
println(OperatingSystem.current().isMacOsX())
println(OperatingSystem.current().isLinux())
}
}
Check out doc for org.gradle.nativeplatform.platform.OperatingSystem interface. It 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 implementation also gives us a hint that we should us 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 not to mess up in case something will change in future.
One can differ the build environment in between Linux, Unix, Windows and OSX - while the Gradle nativeplatform.platform.OperatingSystem differs the target environment (incl. FreeBSD and Solaris), instead.
String osName = org.gradle.internal.os.OperatingSystem.current().getName();
String osVersion = org.gradle.internal.os.OperatingSystem.current().getVersion();
println "*** $osName $osVersion was detected."
if (org.gradle.internal.os.OperatingSystem.current().isLinux()) {
// consider Linux.
} else if (org.gradle.internal.os.OperatingSystem.current().isUnix()) {
// consider UNIX.
} else if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
// consider Windows.
} else if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
// consider OSX.
} else {
// unknown OS.
}
Gradle doesn't provide a public API for detecting the operating system. Hence the os. system properties are your best bet.
Or you can define osName as a String ...
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 ;
}
... and use it later - to include a native library for example:
run {
systemProperty "java.library.path", "lib/$osName"
}
But it wouldn't change anything since OperatingSystem work exactly like your code:
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;
}
}
Edit:
You can do the same for the Arch:
project.ext.osArch = OperatingSystem.current().getArch();
if ("x86".equals(project.ext.osArch)) {
project.ext.osArch = "i386";
}
and:
run {
systemProperty "java.library.path", "lib/$osName/$osArch"
}
Just be aware that getArch() will return:
- "ppc" on PowerPC
- "amd64" on 64b
- "i386" OR "x86" on 32b.
getArch() will return "x86" on Solaris or "i386" for any other platform.
Edit2:
Or if you want to avoid any import, you can simply do it yourself:
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);
}
I don't like detecting OS in Gradle through properties or Ant task, and OperatingSystem class no longer contains current() method.
So, in my opinion, the cleanest way to detect OS would be:
Import DefaultNativePlatform.
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
Then use DefaultNativePlatform in your task.
if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows()) {
println 'Windows'
}
Mind that this method is not ideal as it using Gradle internal API. Tested with Gradle 4.10. I'll put update regarding Gradle 5 later.
참고URL : https://stackoverflow.com/questions/11235614/how-to-detect-the-current-os-from-gradle
'Programing' 카테고리의 다른 글
| jquery로 클릭 한 링크의 href를 얻는 방법은 무엇입니까? (0) | 2020.09.21 |
|---|---|
| 고 루틴 중지 방법 (0) | 2020.09.21 |
| convert_tz는 null을 반환합니다. (0) | 2020.09.21 |
| 새 Mac OS X 터미널 창에서 명령 실행 (0) | 2020.09.21 |
| JavaScript를 사용하여 URL 유효성 검사 시도 (0) | 2020.09.21 |