Gradle task to open url in default browser

How to open url in browser from gradle task?

+9
source share
3 answers

Something like this should do:

task openUrlInBrowser {
   doLast {
       java.awt.Desktop.desktop.browse "http://www.google.com".toURI()
   }
}
+20
source
task showReport(type:Exec) {
  workingDir './build/reports/tests'

  //on windows:
  commandLine 'cmd', '/c', 'start index.html'
}

Then run

gradle showReport

See information on Gradle exec .

+4
source

I made a Windows and Mac support function from Robins answer

def browse(path) {
    def os = org.gradle.internal.os.OperatingSystem.current()
    if (os.isWindows()) {
        exec { commandLine 'cmd', '/c', "start $path" }
    } else if (os.isMacOsX()) {
        exec { commandLine 'open', "$path" }
    }
}

Usage example:

task browseTest {
    doLast {
        def file = project.file('build/reports/tests/testDebugUnitTest/index.html')
        browse file
        browse "https://stackoverflow.com/questions/14847296/gradle-task-to-open-a-url-in-the-default-browser"
    }
}
0
source

All Articles