如何编写一个依赖于另一个插件任务的插件?

时间:2015-05-12 10:32:14

标签: plugins sbt

有一个很棒的sbt插件sbt-dependency-graph,它提供dependencyTree任务来显示依赖项。

我想编写一个取决于它的sbt插件,但总是失败。

build.sbt

sbtPlugin := true

name := "my-sbt-plugin-depends-on-another"

version := "0.1.2.1"

organization := "test20140913"

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.5")

的src /主/阶/ MySbtPlugin.scala

import sbt._

object MySbtPlugin extends AutoPlugin {

  object autoImport {
    lazy val hello = taskKey[Unit]("hello task from my plugin")
    lazy val hello2 = taskKey[Unit]("hello task from my plugin2")
  }

  import autoImport._

  override def trigger = allRequirements

  override def requires = plugins.JvmPlugin

  val helloSetting = hello := println("Hello from my plugin")

  val helloSetting2 = hello2 := {
    println("hello2, task result from another plugins:")
    println(net.virtualvoid.sbt.graph.Plugin.dependencyTree.value)
    println("=========================================")
  }

  override def projectSettings = Seq(
    helloSetting, helloSetting2
  )

}

然后我将它发布到本地,并在另一个项目中使用它:

build.sbt

name := "sbt--plugin-test"

version := "1.0"

scalaVersion := "2.11.6"

net.virtualvoid.sbt.graph.Plugin.graphSettings

项目/ plugins.scala

logLevel := Level.Info

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.5")

addSbtPlugin("test20140913" % "my-sbt-plugin-depends-on-another" % "0.1.2.1")

当我在后期项目上运行sbt时,它会报告:

Reference to undefined setting:

  *:dependencyTree from *:hello2 (/Users/twer/workspace/my-sbt-plugin-depends-on-another/src/main/scala/test20140913/MySbtPlugin.scala:38)
     Did you mean provided:dependencyTree ?

    at sbt.Init$class.Uninitialized(Settings.scala:262)
    at sbt.Def$.Uninitialized(Def.scala:10)
    at sbt.Init$class.delegate(Settings.scala:188)
    at sbt.Def$.delegate(Def.scala:10)

哪里错了?

PS:插件代码在这里:https://github.com/freewind/my-sbt-plugin-depends-on-another

1 个答案:

答案 0 :(得分:3)

dependencyTree仅针对特定配置(好all of them)进行了定义,但它会自动委托给shell中的Compile

尝试定义hello2,如此:

  val helloSetting2 = hello2 := {
    println("hello2, task result from another plugins:")
    import net.virtualvoid.sbt.graph.Plugin.dependencyTree
    println((dependencyTree in Compile).value)
    println("=========================================")
  }