Groovy Grape is Groovy’s dependency manager. A big difference between Grape and other language dependency managers like pip, gem and npm is that, it uses language element(annoation, method call) to describe the dependencies inside a Groovy file, rather than a file out of the program itself like “requirements.txt”, “Gemfile” and “package.json”.
For example, let’s say you have a Groovy environment already, in my case it’s Groovy 2.4.15, create the files below in the same directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now you can run the “server.groovy” in one command like belong, which starts a Jetty server. You can access it in a browser with the URL “http://localhost:8080/index.gsp”.
When running the program for the first time, it may takes a long time like a dozen of seconds. But for the second time or later, it only takes one or two seconds to start. In fact, the reason of first time slow is that, Groovy intepreter reads the Grape configuration in the Groovy file and downloads the dependencies from remote and save to local disk(“~/.groovy/grapes”) if it cannot find the artifact locally. For the second time or later, it will load them from disk directly.
Note the “@Grapes” annotation, that’s where the dependencies information defines. The “group/module/version” are the “groupId/artifactId/version” concepts in Maven. You can ever use shorthand notation like “@Grab(‘org.springframework:spring-orm:3.2.5.RELEASE’)”. More details can be found here.
You can use the “grape list” command to list the locally installed modules. As mentioned, by default the modules are installed at “~/.groovy/grapes/” directory.
You have several ways to use Grape to download the dependencies before running your program. Annotation is one of them. And you can also do it by:
Explicit method call in your program. Code snippet below.
1 2 3 4 5
- Run command line tool.
For me Grape is special because comparing to the equivalents of the other languages, Grape is a feature provided as an ordinary library and can be used as ordinary code inside a program written in the language, instead of an extra part out of the program itself. This fact makes the feature simpler in concept and more flexible to implement interesting things, like customizing your released artifacts without packaging the whole dependencies, just release your pure code logic and download dependencies according to different users’ requirement.
This makes me think of Lisp.