Pure Danger Tech


navigation
home

Using Groovy Grape in groovysh

01 Mar 2010

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.