在 sbt 项目的类路径中添加 tools.jar [英] Add tools.jar in the classpath of sbt project

查看:29
本文介绍了在 sbt 项目的类路径中添加 tools.jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scala 2.9.1 控制台中的:javap"命令需要类路径"中的 tools.jar(来自 JDK6).从 cmd-line 可以使用 '-cp' 参数或 CLASSPATH 环境变量来完成.

The ':javap' command in the scala 2.9.1 console need the tools.jar (from JDK6) in the 'classpath'. From cmd-line it could be done with '-cp' argument or CLASSPATH environment variable.

如何对使用console"和console-quick"命令从 SBT 调用的 scala 控制台执行相同操作?

How to do the same for scala console that invoked from SBT with the 'console' and 'console-quick' commands?

推荐答案

答案很长,可能会在其他地方对您有所帮助.

Long answer, that might help you elsewhere.

如果我想了解 SBT 中的某些内容,我inspect 它:

If I want to know about something in SBT, I inspect it:

> 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

好的,compile:full-classpath 中有一个有趣的依赖项.我希望它是 compile:full-classpath(for console),但它不是.不过,在这种情况下,它不应该给我带来麻烦.让我们检查它.

Ok, there's an interesting dependency in compile:full-classpath. I wish it were compile:full-classpath(for console), but it isn't. It shouldn't cause me trouble in this case, though. Let's inspect it.

> 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, I could go further into the dependencies, but I don't think it's necessary. Let's see what's inside it:

> 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

好的,没有什么意外的.让我们添加 tools.jar.

Ok, nothing unexpected there. Let's add tools.jar.

要更改某些内容,我必须使用 set,并且我必须遵守一堆驼峰式案例和其他规则才能使其正常工作.如果我有 compile:full-classpath(for console),那将变成 fullClasspath in Compile in console.请注意 Compile 中的大写,并且 full-classpath 变为 fullClasspath,以及元素名称的一般重新排序.此处有详细信息.

To change something I have to use set, and I have to respect a bunch of camel case and other rules to get it working. If I had compile:full-classpath(for console), that would become fullClasspath in Compile in console. Note the uppercase in Compile, and that full-classpath became fullClasspath, and the general reordering of element names. Details here.

我认为应该能够获取 show(或者,至少是 inspect)的输出并将其直接反馈给 set,但事实并非如此(无论如何现在),所以只需学习这些转换规则.

I think one should be able to take the output of show (or, at least, inspect) and feed it right back to set, but that's not the case (for now, anyway), so just learn these conversions rules.

我不想重新输入所有内容,只需添加一个 JAR 文件.我需要为此使用 += .用于改变事物的操作符可以在这里找到.

I don't want to retype everything, just add one JAR file. I need to use += for that. The operators used to change thing can be found here.

类路径似乎需要一些 Attributed 的东西.查看 SBT wiki 中 Classpath 上的详细文档,并弄清楚如何提出一个.幸运的是,我可能想要更改的大多数值都没有这个值那么难创建.

The classpath seems to need some Attributed stuff. Check the detailed docs on Classpath in the SBT wiki, and figure how to come up with one. Fortunately, most values I may want to change aren't as difficult to create as this one.

> 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/)

似乎奏效了.让我们 show 确认它的内容,因为即使写 compile 而不是 Compile 也可能让它改变错误的东西.

Seems to have worked. Let's show its content to confirm, since even writing compile instead of Compile might get it to change the wrong thing.

> 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

是的,就是这样.让我们测试一下:

Yep, there it is. Let's test it:

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

成功!!!

当然,这次会议是个谎言.我花了更长的时间才到达那里,但这主要是练习.

Of course, this session is a lie. It took much longer for me to arrive there, but that's mostly practice.

这篇关于在 sbt 项目的类路径中添加 tools.jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆