如何打印依赖jar以在环境变量中使用?

时间:2014-04-25 14:33:49

标签: scala sbt

我想打印依赖项jar,以便我可以将它们放在classpath变量中。

我可以获取依赖jar文件的类路径字符串。

Curenttly,我可以使用compile:dependencyClasspath,但它返回一个需要手动转换为类路径字符串的格式。

$ 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

这就是我将它转换为类路径字符串

的方法
$ 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

如何使用SBT实现类似的功能?

此外,我尝试使用sbt consoleProject尝试创建SBT Task

以下是:

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

现在我无法弄清楚如何使用SBT API来获取文件列表。我已经尝试从这个page了解Classpath。

知道如何获取文件列表吗?

1 个答案:

答案 0 :(得分:14)

任何任务都可以定义单独的日志流(名为export)并将信息转储给它。所有类路径任务都这样做。

sbt "export compile:dependencyClasspath"

sbt "export <...>" > foo应该可以工作,除非你有插件直接写入stdout而不是记录器。

在sbt 0.13.2或0.13.5 IIRC之前,这已被打破,但现在应该可以使用。