如何在SBT命令中添加自定义工件

时间:2015-05-12 13:15:21

标签: scala sbt

有人可以告诉我如何在SBT Artifacts中添加多个Command吗?我想做以下事情。鉴于以下树:

  - target
     |- scala-2.10
         |- classes
             |- my
                 |- package
                     |- foo
                      - bar
                      - john
                      - doe  

我想为Artifact下的每个文件夹创建一个发布个人my/package(jar)的命令。不应该有任何默认Artifact。依赖于此库的项目应使用分类器,即

libraryDependencies += "whatever" % "whatever" % "1.0" Artifacts(Artifact("foo", "john") )

这是我到目前为止所拥有的。

def myCommand = Command.single("myCommand") {
    case (currentState, arg) =>
      val extracted = Project.extract(currentState)

      val compileDirectory = extracted.get[File](classDirectory in Compile)
      val packageDir = new sbt.File(compileDirectory, s"my${File.separator}package")
      val artifactDefinitions = IO.listFiles(packageDir).toList.map {
        serviceDir =>
          val serviceName = serviceDir.name.split(File.separatorChar).reverse.head
          println(s"Adding artifact for $serviceName")

          val serviceTask = taskKey[File](s"Adds a $serviceName artifact")
          serviceTask := {
            serviceDir
          }
          val definition = addArtifact(Artifact("service", serviceName), serviceTask)
          extracted.append(definition.settings, currentState) <-- fails on this line
          definition
      }

      currentState
  }

我收到错误:

Reference to undefined setting: 

  *:serviceTask from *:packagedArtifacts ((sbt.BuildExtra) Defaults.scala:1767)

1 个答案:

答案 0 :(得分:1)

  def myCommand = Command.single("myCommand") {
    case (currentState, arg) =>
      val extracted = Project.extract(currentState)

      val targetDirectory = extracted.get[File](target in Compile)
      val compileDirectory = extracted.get[File](classDirectory in Compile)
      val packageDir = new sbt.File(compileDirectory, s"my${java.io.File.separator}package")
      val additionalArtifacts =  IO.listFiles(packageDir).toList.map {  serviceDir =>
        val serviceName = serviceDir.name.split(java.io.File.separatorChar).reverse.head
        println(s"Adding artifact for $serviceName")

        val files = Path.allSubpaths(serviceDir)
        val outputZip = new sbt.File(targetDirectory, s"$serviceName.jar")
        sbt.IO.zip(files, outputZip)

        (Artifact(serviceName, serviceName), outputZip)
      }.toMap

      val settings = Seq[Setting[_]](
        packagedArtifacts ++= additionalArtifacts,
        artifacts ++= additionalArtifacts.keys.toSeq
      )

      extracted.append(settings, s)
  }