2011-12-07 48 views
18

Lệnh ': javap' trong giao diện điều khiển scala 2.9.1 cần tools.jar (từ JDK6) trong 'classpath'. Từ cmd-line nó có thể được thực hiện với đối số '-cp' hoặc biến môi trường CLASSPATH.Thêm tools.jar vào đường dẫn lớp của dự án sbt

Làm cách nào để thực hiện tương tự cho bàn điều khiển scala được gọi từ SBT bằng lệnh 'console' và 'console-quick'?

Trả lời

31

Câu trả lời dài, có thể giúp bạn ở nơi khác.

Nếu tôi muốn biết về một cái gì đó trong SBT, tôi inspect nó:

> inspect console 
[info] Task: Unit 
[info] Description: 
[info] Starts the Scala interpreter with the project classes on the classpath. 
[info] Provided by: 
[info] {file:/home/dcs/github/anti-xml/}default-39679a/compile:console 
[info] Dependencies: 
[info] compile:compilers(for console) 
[info] compile:full-classpath 
[info] compile:scalac-options(for console) 
[info] compile:initial-commands(for console) 
[info] compile:streams(for console) 
[info] Delegates: 
[info] compile:console 
[info] *:console 
[info] {.}/compile:console 
[info] {.}/*:console 
[info] */compile:console 
[info] */*:console 
[info] Related: 
[info] test:console 

Ok, có một sự phụ thuộc thú vị trong compile:full-classpath. Tôi muốn nó là compile:full-classpath(for console), nhưng nó không phải là. Tuy nhiên, nó không gây rắc rối cho tôi trong trường hợp này. Hãy inspect nó.

> inspect compile:full-classpath 
[info] Task: scala.collection.Seq[sbt.Attributed[java.io.File]] 
[info] Description: 
[info] The exported classpath, consisting of build products and unmanaged and managed, internal and external dependencies. 
[info] Provided by: 
[info] {file:/home/dcs/github/anti-xml/}default-39679a/compile:full-classpath 
[info] Dependencies: 
[info] compile:exported-products 
[info] compile:dependency-classpath 
[info] Reverse dependencies: 
[info] compile:console 
[info] Delegates: 
[info] compile:full-classpath 
[info] *:full-classpath 
[info] {.}/compile:full-classpath 
[info] {.}/*:full-classpath 
[info] */compile:full-classpath 
[info] */*:full-classpath 
[info] Related: 
[info] compile:full-classpath(for doc) 
[info] test:full-classpath 
[info] test:full-classpath(for doc) 
[info] *:full-classpath(for console) 
[info] runtime:full-classpath 
[info] compile:full-classpath(for console) 

Ok, tôi có thể đi xa hơn vào phụ thuộc, nhưng tôi không nghĩ là cần thiết. Hãy xem nội dung bên trong:

> show compile:full-classpath 
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist 
[info] List(Attributed(/home/dcs/github/anti-xml/target/scala-2.9.1/classes), Attributed(/home/dcs/.sbt/boot/scala-2.9.1/lib/scala-library.jar)) 
[success] Total time: 0 s, completed Dec 7, 2011 3:49:30 PM 

Ok, không có gì bất ngờ ở đó. Hãy thêm tools.jar.

Để thay đổi thứ gì đó tôi phải sử dụng set và tôi phải tôn trọng một loạt trường hợp lạc đà và các quy tắc khác để làm cho nó hoạt động. Nếu tôi có compile:full-classpath(for console), điều đó sẽ trở thành fullClasspath in Compile in console. Lưu ý chữ hoa trong Compile và rằng full-classpath đã trở thành fullClasspath và sắp xếp lại các tên phần tử chung. Details here.

Tôi nghĩ ta nên có thể lấy đầu ra của show (hoặc, ít nhất, inspect) và thức ăn nó quay lại ngay để set, nhưng đó không phải là trường hợp (cho bây giờ, dù sao), vì vậy chỉ cần tìm hiểu các quy tắc chuyển đổi .

Tôi không muốn nhập lại mọi thứ, chỉ cần thêm một tệp JAR. Tôi cần sử dụng += cho điều đó. Các toán tử được sử dụng để thay đổi điều có thể được tìm thấy here.

Đường dẫn lớp dường như cần một số công cụ Attributed. Kiểm tra tài liệu chi tiết trên Classpath trong wiki SBT và tìm cách tạo ra tài liệu. May mắn thay, hầu hết các giá trị mà tôi có thể muốn thay đổi không khó để tạo ra như giá trị này.

> set fullClasspath in Compile += Attributed.blank(file("/usr/lib/jvm/java-6-sun-1.6.0.26/lib/tools.jar")) 
[info] Reapplying settings... 
[info] Set current project to anti-xml (in build file:/home/dcs/github/anti-xml/) 

Dường như đã hoạt động. Hãy show nội dung của nó để xác nhận, vì thậm chí viết compile thay vì Compile có thể làm cho nó thay đổi sai.

> show compile:full-classpath                    
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist 
[info] List(Attributed(/home/dcs/github/anti-xml/target/scala-2.9.1/classes), Attributed(/home/dcs/.sbt/boot/scala-2.9.1/lib/scala-library.jar), Attributed(/usr/lib/jvm/java-6-sun-1.6.0.26/lib/tools.jar)) 
[success] Total time: 0 s, completed Dec 7, 2011 3:50:07 PM 

Vâng, đúng vậy. Hãy thử nghiệm nó:

> console 
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist 
[info] Starting scala interpreter... 
[info] 
import com.codecommit.antixml._ 
bookstore: com.codecommit.antixml.Elem = <bookstore><book><title>For Whom the Bell Tolls</title><author>Hemmingway</author></book><book><title>I, Robot</title><author>Isaac Asimov</author></book><book><title>Programming Scala</title><author>Dean Wampler</author><author>Alex Payne</author></book></bookstore> 
books: com.codecommit.antixml.Zipper[com.codecommit.antixml.Elem] = <book><title>For Whom the Bell Tolls</title><author>Hemmingway</author></book><book><title>I, Robot</title><author>Isaac Asimov</author></book><book><title>Programming Scala</title><author>Dean Wampler</author><author>Alex Payne</author></book> 
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> :javap com.codecommit.antixml.Elem 
Compiled from "node.scala" 
public class com.codecommit.antixml.Elem extends java.lang.Object implements com.codecommit.antixml.Node,com.codecommit.antixml.Selectable,scala.ScalaObject,scala.Product,scala.Serializable{ 
    public static final scala.Function1 tupled(); 
    public static final scala.Function1 curry(); 
    public static final scala.Function1 curried(); 
    public static final boolean isValidName(java.lang.String); 
    public scala.collection.Iterator productIterator(); 
    public scala.collection.Iterator productElements(); 
    public java.lang.Object $bslash(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper); 
    public java.lang.Object $bslash$bslash(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper); 
    public java.lang.Object $bslash$bslash$bang(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper); 
    public java.lang.Object select(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper); 
    public com.codecommit.antixml.Zipper toZipper(); 
    public scala.Option prefix(); 
    public java.lang.String name(); 
    public com.codecommit.antixml.Attributes attrs(); 
    public scala.collection.immutable.Map scope(); 
    public com.codecommit.antixml.Group children(); 
    public com.codecommit.antixml.Elem canonicalize(); 
    public java.lang.String toString(); 
    public com.codecommit.antixml.Group toGroup(); 
    public com.codecommit.antixml.Group copy$default$5(); 
    public scala.collection.immutable.Map copy$default$4(); 
    public com.codecommit.antixml.Attributes copy$default$3(); 
    public java.lang.String copy$default$2(); 
    public scala.Option copy$default$1(); 
    public com.codecommit.antixml.Elem copy(scala.Option, java.lang.String, com.codecommit.antixml.Attributes, scala.collection.immutable.Map, com.codecommit.antixml.Group); 
    public int hashCode(); 
    public boolean equals(java.lang.Object); 
    public java.lang.String productPrefix(); 
    public int productArity(); 
    public java.lang.Object productElement(int); 
    public boolean canEqual(java.lang.Object); 
    public com.codecommit.antixml.Elem(scala.Option, java.lang.String, com.codecommit.antixml.Attributes, scala.collection.immutable.Map, com.codecommit.antixml.Group); 
} 

Thành công !!!

Tất nhiên, phiên này là lời nói dối. Mất nhiều thời gian hơn để tôi đến đó, nhưng chủ yếu là thực hành.

+0

Cảm ơn bạn, Daniel, cho câu trả lời chi tiết. –

+0

Với SBT 0.12.0, tôi nhận được: $ sbt console scala> đặt fullClasspath trong Biên dịch + = Attributed.blank (tệp ("/ usr/lib/jvm/jdk1.7.0/lib/tools.jar")) : 1: lỗi: ';' dự kiến ​​nhưng '.' tìm. đặt fullClasspath trong Biên dịch + = Attributed.blank (tệp ("/ usr/lib/jvm/jdk1.7.0/lib/tools.jar")) –

+1

Không chắc chắn phiên bản SBT nào đã xảy ra, nhưng bây giờ phụ thuộc là 'biên dịch: console :: fullClasspath' như bạn muốn. 'fullClasspath trong biên dịch trong giao diện điều khiển' hoạt động. –

6

https://github.com/ensime/ensime-server/blob/master/build.sbt#L35

// epic hack to get the tools.jar JDK dependency 
val JavaTools = List[Option[String]] (
    // manual 
    sys.env.get("JDK_HOME"), 
    sys.env.get("JAVA_HOME"), 
    // osx 
    try Some("/usr/libexec/java_home".!!.trim) 
    catch { 
    case _: Throwable => None 
    }, 
    // fallback 
    sys.props.get("java.home").map(new File(_).getParent), 
    sys.props.get("java.home") 
).flatten.map { n => 
    new File(n + "/lib/tools.jar") 
}.find(_.exists).getOrElse (
    throw new FileNotFoundException (
    """Could not automatically find the JDK/lib/tools.jar. 
     |You must explicitly set JDK_HOME or JAVA_HOME.""".stripMargin 
) 
) 
Các vấn đề liên quan