Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print dependency jars for use in an environment variable?

Tags:

scala

sbt

I wanted to print the dependency jars so that I can put them in a classpath variable.

I can fetch a classpath string for dependency jar files.

Curenttly, I can use compile:dependencyClasspath but it returns a format which needs to be manually transformed into a classpath string.

$ sbt "show compile:dependencyClasspath"

[info] Set current project to scala-snippets (in build file:/home/tuxdna/work/learn/mine/scala-snippets/)
[info] List(Attributed(/home/tuxdna/.ivy2/cache/org.apache.lucene/lucene-core/jars/lucene-core-3.0.1.jar), Attributed(/home/tuxdna/.ivy2/cache/com.netflix.rxjava/rxjava-scala/jars/rxjava-scala-0.15.0.jar), Attributed(/home/tuxdna/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.10.4-RC1.jar), Attributed(/home/tuxdna/.ivy2/cache/com.netflix.rxjava/rxjava-core/jars/rxjava-core-0.15.0.jar), Attributed(/home/tuxdna/.ivy2/cache/com.typesafe.akka/akka-actor_2.10/jars/akka-actor_2.10-2.2.3.jar), Attributed(/home/tuxdna/.ivy2/cache/com.typesafe/config/bundles/config-1.0.2.jar), Attributed(/home/tuxdna/.ivy2/cache/org.scala-lang.modules/scala-async_2.10/bundles/scala-async_2.10-0.9.0-M2.jar), Attributed(/home/tuxdna/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.3.jar), Attributed(/home/tuxdna/.ivy2/cache/org.scala-lang/scala-actors/jars/scala-actors-2.10.2.jar))
[success] Total time: 0 s, completed Dec 17, 2013 4:00:05 PM

This is how I am transforming it into a classpath string

$ sbt "show compile:dependencyClasspath" | grep -B1 'success' | head -1 | ruby -e 's = STDIN.read; puts s[(s =~ /Attributed(.*?,)/)..-1].split(", ").map { |x| x.sub("Attributed(","").gsub(")", "")}.join(":")'

/home/tuxdna/.ivy2/cache/org.apache.lucene/lucene-core/jars/lucene-core-3.0.1.jar:/home/tuxdna/.ivy2/cache/com.netflix.rxjava/rxjava-scala/jars/rxjava-scala-0.15.0.jar:/home/tuxdna/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.10.4-RC1.jar:/home/tuxdna/.ivy2/cache/com.netflix.rxjava/rxjava-core/jars/rxjava-core-0.15.0.jar:/home/tuxdna/.ivy2/cache/com.typesafe.akka/akka-actor_2.10/jars/akka-actor_2.10-2.2.3.jar:/home/tuxdna/.ivy2/cache/com.typesafe/config/bundles/config-1.0.2.jar:/home/tuxdna/.ivy2/cache/org.scala-lang.modules/scala-async_2.10/bundles/scala-async_2.10-0.9.0-M2.jar:/home/tuxdna/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.3.jar:/home/tuxdna/.ivy2/cache/org.scala-lang/scala-actors/jars/scala-actors-2.10.2.jar

How could similar functionality be achieved using SBT?

Also I tried to use sbt consoleProject to try and create a SBT Task.

Here is how:

$ sbt
sbt> consoleProject
scala> val dcc = (dependencyClasspath in Compile)
dcc: sbt.TaskKey[sbt.Keys.Classpath] = sbt.Scoped$$anon$2@3fa1115c

Now I can't figure out how to use SBT API to fetch the list of files. I have already tried to understand the Classpath from this page.

Any idea how to fetch the list of files?

like image 962
tuxdna Avatar asked Apr 25 '14 14:04

tuxdna


1 Answers

Any task can define a separate log stream (named export) and dump information to it. All the classpath tasks do so.

sbt "export compile:dependencyClasspath"

sbt "export <...>" > foo should work, unless you have plugins writing to stdout directly rather than a logger.

This was broken prior to sbt 0.13.2 or 0.13.5 IIRC, but it should work now.

like image 145
jsuereth Avatar answered Nov 01 '22 00:11

jsuereth