获取对 sbt 中 jar 程序集路径的引用

Get reference to jar assembly path in sbt

我正在使用 sbt-izpack to build an installer. It looks like there's a bug where the variable for the package file name isn't being referenced properly. I'm looking to pass in a variable 以便像这样正确打包主 jar:

variables in IzPack += ("artifactName", artifactFileName.value )

问题是我不知道如何获取主要工件的路径字符串。 docs 说我可以映射包并得到一个 (Artifact, File) 对,如下所示:

val artifactFileName = settingKey[String]("My task.")

artifactFileName :=  {
  val (art, file) = packagedArtifact.in(Compile, packageBin).value
  println("Artifact definition: " + art)
  println("Packaged file: " + file.getAbsolutePath)
  file.getAbsolutePath
}

但是 sbt 抱怨 A setting cannot depend on a task

有没有一种方法可以以某种方式获取主程序集的路径,而无需先(在任务中)生成它,以便我可以通过设置传递它?

或者,有没有办法更新任务中提供给 izpack 的设置?

看起来答案在文档中,但并不明显,因为它在关于修改工件的部分中,而不是读取它们的属性。

发件人:http://www.scala-sbt.org/0.12.2/docs/Detailed-Topics/Artifacts.html#modifying-default-artifacts

Each built-in artifact has several configurable settings in addition to publish-artifact. The basic ones are artifact (of type SettingKey[Artifact]), mappings (of type TaskKey[(File,String)]), and artifactPath (of type SettingKey[File]). They are scoped by (<config>, <task>) as indicated in the previous section.

因此您可以获得 artifactPath 的字符串值,这是一个设置,因此可用于 izPack 设置,具有以下内容:

lazy val artifactPathExt = settingKey[String]("Get the main artifact path")
artifactPathExt := (artifactPath in (Compile, packageBin)).value.getPath

虽然我完全忘记了我是如何发现这一点的,但人们可以通过以下方式发现这些信息(sbt 中的可发现性有点问题):

我们知道 package 任务构建了主要输出,因此您可以键入:

inspect tree package

在 sbt 提示符下,显示以下树:

> inspect tree package
[info] compile:package = Task[java.io.File]
[info]   +-compile:packageBin = Task[java.io.File]
[info]     +-compile:packageBin::packageConfiguration = Task[sbt.Package$Conf..
[info]     | +-compile:packageBin::artifactPath = target\scala-2.11\scaladaem..
[info]     | | +-*:scalaBinaryVersion = 2.11
[info]     | | +-*:scalaVersion = 2.11.5

这里可以看到package任务需要compile:packageBin::packageConfiguration。您可以在 sbt 提示符下检查此设置的值。

要在您的构建中真正掌握这个值,您必须知道如何引用这个东西。您必须弄清楚键、任务和 Scopes. You'd have to know the "in" syntax of getting keys out of configs and tasks. Finally, you'd have to know how to declare and use custom tasks 以及在运行时如何引用和声明设置和任务。最后,您必须知道如何使用一个设置来设置另一个设置。

呼。