How do I open a a url in the browser from a gradle task ?
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.
In this quick article, we'll cover creating a “fat jar” in Gradle. Basically, a fat jar (also known as uber-jar) is a self-sufficient archive which contains both classes and dependencies needed to run an application.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With