Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task to open a url in the default browser

How do I open a a url in the browser from a gradle task ?

like image 965
Gautam Avatar asked Feb 13 '13 05:02

Gautam


People also ask

How do I run a default task in Gradle?

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.

What is Gradle Fatjar?

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.


3 Answers

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"
    }
}
like image 135
Love Avatar answered Oct 24 '22 11:10

Love


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.

like image 26
Robin Avatar answered Oct 24 '22 09:10

Robin


Something like this should do:

task openUrlInBrowser {
   doLast {
       java.awt.Desktop.desktop.browse "http://www.google.com".toURI()
   }
}
like image 25
Benjamin Muschko Avatar answered Oct 24 '22 09:10

Benjamin Muschko