Gradle task to open a url in the default browser
I made a function from Robins answer to support windows and mac
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" }
}
}
Example usage:
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"
}
}
task showReport(type:Exec) {
workingDir './build/reports/tests'
//on windows:
commandLine 'cmd', '/c', 'start index.html'
}
Then run
gradle showReport
See the information on Gradle exec.
Something like this should do:
task openUrlInBrowser {
doLast {
java.awt.Desktop.desktop.browse "http://www.google.com".toURI()
}
}