在sbt项目的类路径中添加tools.jar

时间:2011-12-07 16:08:42

标签: scala sbt scala-2.9 javap

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

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

2 个答案:

答案 0 :(得分:31)

答案很长,这可能会帮助你。

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

> 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),但事实并非如此。不过,在这种情况下,它不应该给我带来麻烦。我们inspect吧。

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

好的,我可以进一步了解依赖项,但我不认为这是必要的。让我们看看它里面有什么:

> 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

要改变我必须使用set的东西,我必须尊重一堆骆驼案和其他规则才能使它运转起来。如果我有compile:full-classpath(for console),那将成为fullClasspath in Compile in console。请注意Compile中的大写字母,full-classpath变为fullClasspath,以及元素名称的一般重新排序。 Details here

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

我不想重新输入所有内容,只需添加一个JAR文件即可。我需要使用+=。用于更改事物的运算符可以找到here

类路径似乎需要一些Attributed的东西。查看SBT wiki中Classpath上的详细文档,并想出如何提出一个。幸运的是,我可能想要改变的大多数值并不像这个那样难以创建。

> 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,也可能会改变错误的内容。

> 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
是的,就是这样。我们来测试一下:

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

成功!!!

当然,本次会议是一个谎言。到达那里我需要更长的时间,但这主要是练习。

答案 1 :(得分: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
  )
)