A Question Asker
A Question Asker

Reputation: 3311

Can sbt be used to return the path to a given version of the scala-library jar?

My goal is something like

# returns /home/user/.sbt/0.12.4/boot/scala-2.9.3/lib/scala-library.jar
./sbt ++2.9.3 library-jar

Is something like this possible?

Upvotes: 1

Views: 69

Answers (1)

gourlaysama
gourlaysama

Reputation: 11280

There is a scalaInstance key in SBT that contains (among other things) the library jar path:

$ sbt ++2.9.3 "show scalaIntance"
[info] Loading global plugins from /home/gourlaysama/.sbt/0.13/plugins
[info] Set current project to tmp (in build file:/tmp/)
[info] Setting version to 2.9.3
[info] Set current project to tmp (in build file:/tmp/)
[info] Updating {file:/tmp/}tmp...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Scala instance{version label 2.9.3, actual version 2.9.3, library jar: /home/gourlaysama/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.9.3.jar, compiler jar: /home/gourlaysama/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.9.3.jar}
[success] Total time: 1 s, completed May 8, 2014 11:09:56 PM

Then you would have to post-process that output to extract the library jar path. Note that this will download the jars if they don't exist.

You can actually get the exact String using consoleProject (starting a scala console inside the SBT project), but I don't think there is a way to do it all in one command from your shell:

$ sbt
[info] Loading global plugins from /home/gourlaysama/.sbt/0.13/plugins
[info] Set current project to tmp (in build file:/tmp/)
> ++2.9.3
[info] Setting version to 2.9.3
[info] Set current project to tmp (in build file:/tmp/)
> consoleProject
[info] Starting scala interpreter...
[info] 
import sbt._
import Keys._
import _root_.org.sbtidea.SbtIdeaPlugin._
import _root_.sbt.plugins.IvyPlugin
import _root_.sbt.plugins.JvmPlugin
import _root_.sbt.plugins.CorePlugin
import _root_.sbt.plugins.JUnitXmlReportPlugin
import currentState._
import extracted._
import cpHelpers._
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> println(scalaInstance.eval.libraryJar)
/home/gourlaysama/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.9.3.jar

The best way I can find to do the above from outside SBT is to exploit initialCommands, as in:

sbt "set initialCommands in consoleProject := \"println(scalaInstance.eval.libraryJar); sys.exit()\"" "consoleProject"

This is ugly, but it actually works. Of course there are still quite a lot of useless output lines before and after the jar path.

Upvotes: 2

Related Questions