How to detect the current OS from Gradle

前端 未结 6 1869
囚心锁ツ
囚心锁ツ 2020-12-08 17:57

I found this answer about how to do it with Groovy:

Detecting the platform (Window or Linux) by Groovy/Grails:



        
6条回答
  •  离开以前
    2020-12-08 18:27

    One can differentiate the build environment in between Linux, Unix, Windows and OS X - while the Gradle nativeplatform.platform.OperatingSystem differentiates the target environment (incl. FreeBSD and 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.
    }
    

    One can also use an Ant task (source):

    import org.apache.tools.ant.taskdefs.condition.Os
    
    task checkWin() << {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            // Consider Windows.
        }
    }
    

提交回复
热议问题