从 sbt 中的依赖项获取资源文件

Get resource file from dependency in sbt

我有一个 sbt-web project with some webjar and common jar dependencies. I want to get resource file from one of my jar dependency and use it in concatenation 任务。但是我不知道如何在我的 build.sbt.

中引用依赖 jar 中的资源

通常 getResourceAsStream 有效:

getClass.getResourceAsStream("/path/to/resource/in/jar")

将资源作为流读取很重要getClass.getResource将不起作用。

我终于在 this 文档中找到了解决方案。主要思想是在类路径依赖项中找到正确的 jar,将其解压缩到临时文件夹,然后使用这些文件执行您需要的操作。在我的例子中,我将它复制到我的目标目录并在连接任务中使用它。

我最终得到了这段代码:

def copyResourceFromJar(classpathEntry: Attributed[File], jarName: String, resourceName: String) = {
  classpathEntry.get(artifact.key) match {
    case Some(entryArtifact) => {
      // searching artifact
      if (entryArtifact.name.startsWith(jarName)) {
        // unpack artifact's jar to tmp directory
        val jarFile = classpathEntry.data
        IO.withTemporaryDirectory { tmpDir =>
          IO.unzip(jarFile, tmpDir)
          // copy to project's target directory
          // Instead of copying you can do any other stuff here 
          IO.copyFile(
            tmpDir / resourceName,
            (WebKeys.webJarsDirectory in Assets).value / resourceName
          )
        }
      }
    }
    case _ =>
  }
}
for(entry <- (dependencyClasspath in Compile).value) yield {
  copyResourceFromJar(entry, "firstJar", "firstFile.js")
  copyResourceFromJar(entry, "secondJar", "some/path/secondFile.js")
}

这段代码应该放在任务中。例如:

val copyResourcesFromJar = TaskKey[Unit]("copyResourcesFromJar", "Copy resources from jar dependencies")
copyResourcesFromJar := {
  //your task code here
}
copyResourcesFromJar <<= copyResourcesFromJar dependsOn (dependencyClasspath in Compile)

并且不要忘记将此任务作为依赖项添加到您的构建任务中。就我而言,它看起来像这样:

concat <<= concat dependsOn copyResourcesFromJar

其他答案对我不起作用,我想是因为它们在旧版本的 SBT 上。一段时间以来,我一直在努力反对这个问题,但我终于有了一些工作。这是在 SBT v1.5.5 上的任意编译任务(我的是 guardrail)之前执行此资源复制的完整脚本:

lazy val myDep = "my-dep-project"
lazy val myFilename = "someDir/myFile.yaml"

val copyResourcesFromJars =
  TaskKey[Unit]("copyResourcesFromJars", "Copy specific resources to be used by this project")

copyResourcesFromJars := {
  def copyResourceFromJar(classpathEntry: Attributed[File], jarName: String, resourceName: String): Unit = {
    classpathEntry.get(artifact.key) match {
      case Some(entryArtifact) =>
        // searching artifact
        if (entryArtifact.name.startsWith(jarName)) {
          // unpack artifact's jar to tmp directory
          val jarFile = classpathEntry.data
          IO.withTemporaryDirectory { tmpDir =>
            IO.unzip(jarFile, tmpDir)
            // copy to project's target directory
            // Instead of copying you can do any other stuff here
            IO.copyFile(
              tmpDir / resourceName,
              baseDirectory.value / s"target/$resourceName"
            )
          }
        }
      case _ =>
    }
  }

  (Compile / dependencyClasspath).value.foreach(entry =>
    copyResourceFromJar(entry, myDep, myFilename)
  )
}

Compile / guardrail := (Compile / guardrail dependsOn copyResourcesFromJars).value

(project in file(".")).settings(
  ...
  Compile / guardrailTasks := List(
    ScalaServer(
      baseDirectory.value / s"target/$myFilename",
      pkg = "my.pkg.guardrail"
    )
  )
)