views:

4014

answers:

7

I've tried to use the new Groovy Grape capability in Groovy 1.6-beta-2 but I get an error message;

unable to resolve class com.jidesoft.swing.JideSplitButton

from the Groovy Console (/opt/groovy/groovy-1.6-beta-2/bin/groovyConsole) when running the stock example;

import com.jidesoft.swing.JideSplitButton
@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,)')
public class TestClassAnnotation {
    public static String testMethod () {
        return JideSplitButton.class.name
    }
}

I even tried running the grape command line tool to ensure the library is imported. Like this;

 $ /opt/groovy/groovy-1.6-beta-2/bin/grape install com.jidesoft jide-oss

which does install the library just fine. How do I get the code to run/compile correctly from the groovyConsole?

+3  A: 

There is still some kinks in working out the startup/kill switch routine. For Beta-2 do this in it's own script first:

groovy.grape.Grape.initGrape()

Another issue you will run into deals with the joys of using an unbounded upper range. Jide-oss from 2.3.0 onward has been compiling their code to Java 6 bytecodes, so you will need to either run the console in Java 6 (which is what you would want to do for Swing anyway) or set an upper limit on the ranges, like so

import com.jidesoft.swing.JideSplitButton

@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,2.3.0)')
public class TestClassAnnotation {
    public static String testMethod () {
        return JideSplitButton.class.name
    }
}

new TestClassAnnotation().testMethod()
shemnon
A: 

Ok. Seems like this a short working demo (running from the groovyConsole)

groovy.grape.Grape.initGrape()
@Grab(group='com.jidesoft', module='jide-oss', version='[2.2.1,2.3.0)')
public class UsedToExposeAnnotationToComplier {}
com.jidesoft.swing.JideSplitButton.class.name

When run it produces

Result: "com.jidesoft.swing.JideSplitButton"

Very cool!!

Bob Herrmann
A: 

Different example using latest RC-2 (note: Grab annotates createEmptyInts):

// create and use a primitive array
import org.apache.commons.collections.primitives.ArrayIntList

@Grab(group='commons-primitives', module='commons-primitives', version='1.0')
def createEmptyInts() { new ArrayIntList() }

def ints = createEmptyInts()
ints.add(0, 42)
assert ints.size() == 1
assert ints.get(0) == 42
Examples stolen from http://groovy.codehaus.org/Grape. (copied exactly)Also, it does not answer the question.
Pablo Fernandez
A: 

Another example (note: Grab annotates getHtml):

// find the PDF links in the Java 1.5.0 documentation
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='0.9.7')
def getHtml() {
    def parser = new XmlParser(new org.ccil.cowan.tagsoup.Parser())
    parser.parse("http://java.sun.com/j2se/1.5.0/download-pdf.html")
}
html.body.'**'[email protected](~/.*\.pdf/).each{ println it }
Examples stolen from http://groovy.codehaus.org/Grape. (copied exactly)Also, it does not answer the question.
Pablo Fernandez
A: 

Another example (note: Grab annotates getFruit):

// Google Collections example
import com.google.common.collect.HashBiMap
@Grab(group='com.google.code.google-collections', module='google-collect', version='snapshot-20080530')
def getFruit() { [grape:'purple', lemon:'yellow', orange:'orange'] as HashBiMap }
assert fruit.inverse().yellow == 'lemon'
Examples stolen from http://groovy.codehaus.org/Grape. (copied exactly)Also, it does not answer the question.
Pablo Fernandez
+2  A: 

I finally got it working for Groovy Shell (1.6.5, JVM: 1.6.0_13). This should be documented better.

First at the command line...

grape install org.codehaus.groovy.modules.http-builder http-builder 0.5.0-RC2

Then in groovysh...

groovy:000> import groovy.grape.Grape
groovy:000> Grape.grab(group:'org.codehaus.groovy.modules.http-builder', module:'http-builder', version:'0.5.0-RC2')
groovy:000> def http= new groovyx.net.http.HTTPBuilder('http://rovio')
===> groovyx.net.http.HTTPBuilder@91520

The @grab is better used in a file than the shell.

Jim Morris
A: 

Hi,

I seem to have problems using Grape no matter how I do it. From within Idea I made a Groovy script:


import groovy.grape.Grape

Grape.grab(group:'org.codehaus.groovy.modules.http-builder', module:'http-builder', version:'0.5.0-RC2')

//ALSO tried: @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )

def http = new groovyx.net.http.HTTPBuilder( 'http://www.google.com/search' )

http.request(groovyx.net.http.Method.GET,groovyx.net.http.ContentType.TEXT) { req -> uri.path = '/mail/help/tasks/' // overrides any path in the default URL headers.'User-Agent' = 'Mozilla/5.0'

response.success = { resp, reader -> assert resp.statusLine.statusCode == 200 println "My response handler got response: ${resp.statusLine}" println "Response length: ${resp.headers.'Content-Length'}" System.out << reader // print response stream }

// called only for a 401 (access denied) status code: response.'404' = { resp -> println 'Not found' } }


Problem is that the code doesn't compile, as the Idea compiler immediately recognize something it doesn't like: the groovyx package.

Does anyone have a solution for running Grape within Idea (actually within a Grails project, but as a independent script)

Thaks in advance,

Christian Sonne Jensen

Christian Sonne Jensen
You can edit the run configuration and uncheck the 'make' box. This will prevent intellij from trying to compile your project before running the script.