groovydependenciesgebgroovy-grape

What is happening behind the scene when running this Groovy script?


@Grapes([
    @Grab("org.codehaus.geb:geb-core:0.7.2"),
    @Grab("org.seleniumhq.selenium:selenium-firefox-driver:2.15.0"),
    @Grab("org.seleniumhq.selenium:selenium-support:2.15.0")
])
import geb.Browser

Browser.drive {
  // Load the page
  go "http://www.whu.edu.cn"

  // $("a") returns all hyperlinks on the page, similar to jQuery
  $("a").each { a ->
     // Display the required link properties and attributes
     println """
        The link to '${a.@href}' with text '${a.text()}' is at location (${a.x}, ${a.y}),
        with a height of ${a.height}px and a width of ${a.width}px.
     """
  }
}

I just created my first Groovy project in Eclipse and created my first Groovy class inside the project. Everything written for the class is as above. When I ran the script, it didn't throw any error nor would it terminate in time.

Was it trying to download all the annotated dependencies? If so, does it need to download the dependencies each time it is run? Or it is once and for all?


Solution

  • When you run this, it will check to see if the correct version of each of the libraries annotated with @Grab has been downloaded, and if not, will attempt to download it. And it's not just the named libraries, it's also those libraries' dependencies.

    So, yes, it can take some time to run this the first time. Subsequent runs should take much less time.

    Note that this is just a convenience. You can also download the required libraries, and specify them in the -classpath argument to the 'groovy' command (and remove the Grapes/Grab construct).

    For more info, see http://groovy.codehaus.org/Grape