How to detect the current OS from Gradle

前端 未结 6 1874
囚心锁ツ
囚心锁ツ 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

    I don't like detecting the OS in Gradle through properties or an Ant task, and the OperatingSystem class no longer contains the current() method.

    So, in my opinion, the cleanest way to detect the 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 is using the Gradle internal API.

    It was tested with Gradle 4.10.

提交回复
热议问题