2008-10-10 22 views
12

Tôi đã cố gắng sử dụng khả năng mới Groovy Grape trong Groovy 1.6-beta-2 nhưng tôi nhận được thông báo lỗi;Nhận nho của Groovy!

unable to resolve class com.jidesoft.swing.JideSplitButton 

từ Groovy Console (/opt/groovy/groovy-1.6-beta-2/bin/groovyConsole) khi chạy ví dụ chứng khoán;

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

Tôi thậm chí đã thử chạy công cụ dòng lệnh nho để đảm bảo thư viện được nhập. Như thế này;

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

cài đặt thư viện nào tốt. Làm cách nào để tôi chạy mã/biên dịch một cách chính xác từ groovyConsole?

Trả lời

5

Vẫn còn một số gợi ý khi làm việc trong quy trình chuyển đổi khởi động/diệt. Đối với Beta-2, hãy thực hiện điều này trong tập lệnh riêng của nó trước tiên:

groovy.grape.Grape.initGrape() 

Một vấn đề khác bạn sẽ gặp phải với niềm vui khi sử dụng dải trên không bị giới hạn. Jide-oss từ 2.3.0 trở đi đã được biên dịch mã của họ sang Java bytecodes, vì vậy bạn sẽ cần chạy một giao diện điều khiển trong Java 6 (đó là những gì bạn sẽ muốn làm cho Swing anyway) hoặc thiết lập một giới hạn trên trên phạm vi, như vậy

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

Ok. Có vẻ như đây là một bản demo làm việc ngắn (chạy từ 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 

Khi chạy nó tạo

Kết quả: "com.jidesoft.swing.JideSplitButton"

Rất mát mẻ !!

+0

Điều này rất hữu ích cho tôi. Lớp trống là chìa khóa. Một vài điểm ... 1/Tôi không cần dòng initGrape() (sử dụng groovy 1.7x hoặc 1.8x) .. 2/Nếu bạn đứng sau proxy, hãy đảm bảo thêm cài đặt proxy của bạn -Dhttp.proxyHost = -Dhttp.proxyPort = ... Tôi thêm chúng vào startGroovy.sh | bat – khylo

-1

dụ khác nhau sử dụng mới nhất RC-2 (lưu ý: 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 
+1

Ví dụ bị lấy cắp từ http://groovy.codehaus.org/Grape. (được sao chép chính xác) Ngoài ra, nó không trả lời câu hỏi. –

+1

Tôi đồng ý. Vui lòng giới thiệu mọi người để sửa câu trả lời nếu bạn tìm thấy câu trả lời ở nơi khác trên web. Cung cấp liên kết. Vui lòng không nhận tín dụng cho công việc của người khác mà không có ghi nhận tác giả. – Anarchofascist

-1

Một ví dụ khác (lưu ý: 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 } 
+0

Ví dụ bị lấy cắp từ http://groovy.codehaus.org/Grape. (được sao chép chính xác) Ngoài ra, nó không trả lời câu hỏi. –

-3

Một ví dụ khác (lưu ý: 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' 
+0

Ví dụ bị lấy cắp từ http://groovy.codehaus.org/Grape. (được sao chép chính xác) Ngoài ra, nó không trả lời câu hỏi. –

5

Cuối cùng tôi cũng đã làm việc này r Groovy Shell (1.6.5, JVM: 1.6.0_13). Điều này sẽ được ghi lại tốt hơn.

đầu tiên tại dòng lệnh ...

nho cài đặt org.codehaus.groovy.modules.http-builder http-builder 0.5.0-RC2

Sau đó, trong 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 được sử dụng tốt hơn trong tệp hơn vỏ.

0

Báo cáo nhập phải xuất hiện sau các lần lấy.
Ps. Ít nhất một câu lệnh nhập phải tồn tại sau khi lấy

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