如何显示用于运行任务的类路径?

时间:2014-02-11 09:44:23

标签: sbt

用于run任务的类路径如何在SBT 0.13下显示?

我在https://groups.google.com/forum/#!msg/simple-build-tool/0rhVRPnjyZU/DOYAd14gh1wJ找到了一些信息:

  

我倾向于使用类路径作为最近对我的构建进行故障排除的方法,也许这个任务也可以帮助你:

lazy val printClasspath = task {
  this.runClasspath.getPaths.foreach(println);
  None
}
     

runClasspath是一个PathFinder [1]实例,您可以进一步询问它。我想构建一个':' - 这些路径的分隔列表很容易。

我不明白我应该在哪里以及如何使用这个提示。请指教。

4 个答案:

答案 0 :(得分:40)

如果我在sbt shell中输入:

inspect run

我看到,除了其他输出之外:

[info] Dependencies:
[info]  runtime:fullClasspath

那么如果我输入:

show runtime:fullClasspath

我输出如下:

 List(
   Attributed(/Users/tisue/Dropbox/repos/euler/target/scala-2.10/classes),
   Attributed(/Users/tisue/.sbt/boot/scala-2.10.3/lib/scala-library.jar))

你可能正在寻找什么?

您也可以在冒号分隔的表单中获取它,该表单适合在命令行中与java -classpath一起使用:

export runtime:fullClasspath

打印例如:

/Users/tisue/Dropbox/repos/euler/target/scala-2.10/classes:/Users/tisue/.sbt/boot/scala-2.10.3/lib/scala-library.jar

答案 1 :(得分:13)

tl; dr 使用last run或撰写自定义任务。

上次运行

使用last run,您将看到整个类路径:

[info] Running main.Main
[debug] Waiting for threads to exit or System.exit to be called.
[debug]   Classpath:
[debug]     /Users/jacek/work/ingrifo/dictionary-spray/target/scala-2.11/classes
[debug]     /Users/jacek/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.4.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-can_2.11/bundles/spray-can_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-io_2.11/bundles/spray-io_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-util_2.11/bundles/spray-util_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-http_2.11/bundles/spray-http_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/org.parboiled/parboiled-scala_2.11/bundles/parboiled-scala_2.11-1.1.6.jar
[debug]     /Users/jacek/.ivy2/cache/org.parboiled/parboiled-core/bundles/parboiled-core-1.1.6.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-routing_2.11/bundles/spray-routing_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-httpx_2.11/bundles/spray-httpx_2.11-1.3.2.jar
[debug]     /Users/jacek/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.2.jar
[debug]     /Users/jacek/.ivy2/cache/org.jvnet.mimepull/mimepull/jars/mimepull-1.9.4.jar
[debug]     /Users/jacek/.ivy2/cache/com.chuusai/shapeless_2.11/jars/shapeless_2.11-1.2.4.jar
[debug]     /Users/jacek/.ivy2/cache/io.spray/spray-json_2.11/bundles/spray-json_2.11-1.3.1.jar
[debug]     /Users/jacek/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.3.6.jar
[debug]     /Users/jacek/.ivy2/cache/com.typesafe/config/bundles/config-1.2.1.jar
[debug] Waiting for thread run-main-0 to terminate.
[debug]     Thread run-main-0 exited.
[debug] Waiting for thread default-akka.actor.default-dispatcher-4 to terminate.

转储CLASSPATH的自定义任务

如果您需要查看run任务所依赖的内容,请使用inspect

[fullclasspath]> inspect run
[info] Input task: Unit
[info] Description:
[info]  Runs a main class, passing along arguments provided on the command line.
[info] Provided by:
[info]  {file:/Users/jacek/sandbox/so/fullClasspath/}fullclasspath/compile:run
[info] Defined at:
[info]  (sbt.Defaults) Defaults.scala:704
[info] Dependencies:
[info]  compile:run::streams
[info]  runtime:fullClasspath
[info]  compile:run::runner
[info]  compile:run::mainClass
[info] Delegates:
[info]  compile:run
[info]  *:run
[info]  {.}/compile:run
[info]  {.}/*:run
[info]  */compile:run
[info]  */*:run
[info] Related:
[info]  test:run

Dependencies中有runtime:fullClasspath设置,这意味着run取决于其值。

根据SBT helpfullClasspath是......

[fullclasspath]> help fullClasspath
The exported classpath, consisting of build products and unmanaged and managed, internal and external dependencies.

您只需要解析runtime:fullClasspath设置的值。

[fullclasspath]> show runtime:fullClasspath
[info] Updating {file:/Users/jacek/sandbox/so/fullClasspath/}fullclasspath...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] List(Attributed(/Users/jacek/sandbox/so/fullClasspath/target/scala-2.10/classes), Attributed(/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar))
[success] Total time: 0 s, completed Feb 13, 2014 1:27:38 AM

要获得更易解析的输出,请使用以下fullClasspath作为runtime范围(Scala / SBT代码中的Runtime)定义的lazy val printClasspath = taskKey[Unit]("Dump classpath") printClasspath := { (fullClasspath in Runtime value) foreach { e => println(e.data) } } 任务:

Runtime

不幸的是,{{1}}配置是硬编码的,认为接受配置的输入任务会使它变得更好。

答案 2 :(得分:10)

或直接来自bash,dash,sh或任何shell:

 sbt 'inspect run' 'show runtime:fullClasspath'

 sbt "inspect run" "show runtime:fullClasspath"

用于Windows命令行。

答案 3 :(得分:2)

在build.sbt文件中添加第lazy val printClasspath ...行。

然后在命令行运行:

sbt printClasspath