【问题标题】:How to create sbt plugin for current events of building app?如何为构建应用程序的当前事件创建 sbt 插件?
【发布时间】:2019-01-10 17:30:29
【问题描述】:

我有这个 hello world 插件:

object HelloPlugin extends AutoPlugin {

  object autoImport {
    val sayHello: TaskKey[Unit] = TaskKey("saying hello")
  }

  import autoImport._
  override def projectSettings = Seq(

    sayHello := {
      println("------------------------------------ heeeeeeeeeeeeeeeelooooooooo -------------------")
    }
  )

}

我想在编译前后使用我的sayHello 任务。 我该怎么做?

我找到了relative question,但这与 AutoPlugin 扩展无关。

【问题讨论】:

    标签: java scala sbt


    【解决方案1】:

    您只需要根据您的情况调整该答案即可。答案保持不变:使用dependsOn,但不是build.sbt,而是将其添加到插件的projectSettings

    override def projectSettings = Seq(
      sayHello := { ... },
      Compile/compile := (Compile/compile).dependsOn(sayHello).value
    )
    

    【讨论】:

    • 太棒了!但我不明白如何导入Compile/compileIntelliJ Idea 帮不了我。 :( 另外,我也可以在编译后做点什么吗?
    • “导入Compile/compile”是什么意思?如果您使用的是 sbt 0.13,请改写 compile in Compile。要在完成任务后做某事,请阅读以下内容:scala-sbt.org/release/docs/Howto-Sequential-Task.html
    【解决方案2】:

    你需要覆盖compileTaskKey,比如:

    override def projectSettings: Seq[Def.Setting[_]] = Seq(
        compile in Compile := Def.taskDyn {
              hello.value // call before
              val c = (compile in Compile).value // actual compilation
              Def.task {
                hello.value // call after
                c
              }
            }.value
    )
    
    def hello: Def.Initialize[Task[Unit]] = Def.task {
       println("hello")
    }
    

    文档:https://www.scala-sbt.org/1.0/docs/Howto-Dynamic-Task.html#build.sbt+v2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 2013-01-22
      • 2015-09-01
      • 2020-04-03
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多