Using Groovy Grape in groovysh

9

In prepping a talk on GPars, the Groovy concurrency library, I’ve been using Grape for dependency management and I must say it’s mad fun. I’m sure there must be some downside or something complicated about it but I haven’t run into it yet!

To get started, you need to set up your .groovy/grapeConfig.xml. I copied this from the Grape page linked above:

<ivysettings>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
    <chain name="downloadGrapes">
      <filesystem name="cachedGrapes">
        <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
        <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
      </filesystem>
      <!-- todo add 'endorsed groovy extensions' resolver here -->
      <ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
      <ibiblio name="ibiblio" m2compatible="true"/>
      <ibiblio name="java.net2" root="http://download.java.net/maven/2/" m2compatible="true"/>
      <ibiblio name="jboss" root="http://repository.jboss.org/maven2/" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>

To use it in groovysh, just do this:

groovy:000> import groovy.grape.Grape
===> [import groovy.grape.Grape]
groovy:000> Grape.grab(group:'net.homeip.yusuke', module:'twitter4j', version:'2.0.10')
===> null
groovy:000> import twitter4j.Twitter
===> [import groovy.grape.Grape, import twitter4j.Twitter]
groovy:000> new Twitter('user', 'password').
     updateStatus('holy cow I can tweet from groovysh!')

If you want to use Grape in your app, it’s easy to just add an annotation into the code:

@Grab(group='net.homeip.yusuke', module='twitter4j', version='2.0.10')

In summary, I like the Grape.

Comments

9 Responses to “Using Groovy Grape in groovysh”
  1. Buddy Casino says:

    It’s not that I don’t hate Maven, I do.
    But is there any way to have configuration inheritance? Its the feature that I really love in Maven, because it enables me to keep my build configs DRY.

  2. Matt Passell says:

    @Buddy I’m not sure if it supports config inheritance, but the place to look would probably be the docs for Apache Ivy (http://ant.apache.org/ivy/), since that’s what Grape uses behind the scenes. Keep in mind that the main point of Grape is to let you quickly bang out scripts that internally describe all the libraries needed to run them – nicely self-contained. If you’re looking for a full-blown build system/language, Gradle (which also uses Ivy for dependency management) is really cool. (http://gradle.org/)

    By the way, if you don’t need to tweak the default configuration (I see that Alex added a reference to the JBoss repo), you don’t need to provide a config file at all.

    –Matt

  3. Anonymous says:

    I just tried your example and had to import twitter4j.* also

  4. Paul King says:

    In source files you can use the abbreviated form too:
    @Grab(‘net.homeip.yusuke:twitter4j:2.0.10′)
    We should make Grape.grab support that too.

  5. Taterpeel says:

    @Buddy @Matt – these are referred to as transitive dependencies. I haven’t used Ivy directly, but looking at the docs, it looks like it handles transitive dependencies by default. you have to set transitive=”false” to explicitly turn it off for a jar.

    http://ant.apache.org/ivy/history/latest-milestone/ivyfile/dependency.html

    Also, Gradle uses Ivy for it’s dependency management.

    one more thing. check this post from mr. haki for a nice script that uses grape to grab the jetty jar and runs an embedded jetty webserver. I’ve started using that when I feel like Websphere is getting in my way.

    http://mrhaki.blogspot.com/2009/10/groovy-goodness-groovlets-as.html

  6. Ingo Richter says:

    Grape is really cool. BTW: Are you sure that you need to create the grapeConfig.xml? I don’t have one and it’s working w/o it. I thought you need it if you require a different repository.
    Thanks.

  7. Alex says:

    @Ingo: As Matt indicated above, you do not need to create a grapeConfig.xml. However, I needed to add a repository so I did need to do it in my example.

  8. Ingo Richter says:

    @Alex: alright, it seems that I missed that point from Matt, sorry. IMHO the more elegant solution (when using groovy 1.7.x) would be to use the @GrabResolver to make things more self-contained w/o tweaking the local settings file.