2008-10-10 92 views
12

我试过在Groovy 1.6-beta-2中使用新的Groovy Grape功能,但我收到一条错误消息;获取Groovy的葡萄!

unable to resolve class com.jidesoft.swing.JideSplitButton 

从Groovy的控制台(/opt/groovy/groovy-1.6-beta-2/bin/groovyConsole)运行库存例如当;

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 
    } 
} 

我甚至尝试运行grape命令行工具来确保库已导入。喜欢这个;

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

哪个安装库就好了。我如何从groovyConsole中正确运行/编译代码?

回答

5

在制定启动/终止开关程序方面还存在一些问题。对于β-2这样做在自己的脚本首先:

groovy.grape.Grape.initGrape() 

另外一个问题,你会碰到与使用无界上限的乐趣交易。从2.3.0开始,Jide-oss一直在将它们的代码编译为Java 6字节码,因此您需要在Java 6中运行控制台(无论如何您都希望这样做),或者设置上限范围,像这样

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() 
2

好的。看起来这很短的工作演示(从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 

运行时它产生

结果: “com.jidesoft.swing.JideSplitButton”

非常酷!

+0

这对我很好。空课是关键。几点... 1 /我不需要initGrape()行(使用groovy 1.7x或1.8x).. 2 /如果您在代理后面请确保添加您的代理设置-Dhttp.proxyHost = -Dhttp.proxyPort = ...我将它们添加到我的开始处Groovy.sh | bat – khylo 2012-03-05 09:31:38

-1
采用最新RC-2

不同的例子(注:抓住诠释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 
+1

从http://groovy.codehaus.org/Grape中盗取的示例。 (完全复制) 此外,它不回答问题。 – 2009-04-23 20:09:07

+1

我同意。如果您在网络上的其他位置找到正确答案,请向他们推荐。提供一个链接。请不要在没有归属的情况下为其他人的工作留下好印象。 – Anarchofascist 2013-07-15 23:28:55

-1

另一个例子(注:抓住诠释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 } 
+0

从http://groovy.codehaus.org/Grape中盗取的示例。 (完全复制) 此外,它不回答问题。 – 2009-04-23 20:10:12

-3

另一个例子(注:Grab诠释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' 
5

我终于搞定了fo r Groovy Shell(1.6.5,JVM:1.6.0_13)。这应该记录得更好。

首先在命令行...

葡萄安装org.codehaus.groovy.modules.http建设者HTTP建设者0.5.0-RC2

然后在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') 
===> [email protected] 

@grab比shell更适合在文件中使用。

0

导入语句必须在之后出现
Ps。至少一个进口声明必须存在之后

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