Adding new task dependencies to built-in SBT tasks?

前端 未结 4 2002
耶瑟儿~
耶瑟儿~ 2021-02-01 03:43

Is it possible to override or modify built-in SBT tasks (like compile) to depend on custom tasks in my own Build.scala? Overriding e.g. \"compile\" directly is not possible sinc

相关标签:
4条回答
  • 2021-02-01 04:11

    Update: See arussell84's answer for a modern way to do this

    You should be able to do it like this:

    in a .sbt file:

    compile <<= (compile in Compile) dependsOn jruby
    

    Where jruby is a task key that you've defined in a project/something.scala file:

    val jruby = TaskKey[Unit]("jruby", "run a jruby file")
    

    Also, this isn't part of your question but you can just call regular Scala code:

    compile <<= (compile in Compile) map { result =>
      println("in compile, something")
      result
    }
    
    0 讨论(0)
  • 2021-02-01 04:19

    In the base_dir/project/ folder create a file build.sbt and put libraryDependencies += ... there.

    That's the idiomatic SBT way to build your "build project", also known as "Meta Build".

    0 讨论(0)
  • 2021-02-01 04:21

    Reply to self: http://code.google.com/p/simple-build-tool/wiki/ProjectDefinitionExamples#Insert_Task_Dependency tells the answer:

    If you are using older 0.7.x SBT versions you can do this:

    import sbt._
    class SampleProject(info: ProjectInfo) extends DefaultProject(info) {
      lazy val printAction = task { print("Testing...") }
      override def compileAction = super.compileAction dependsOn(printAction)
    }
    
    0 讨论(0)
  • 2021-02-01 04:24

    Since this question appears when Googling how to add a dependency in SBT, and the current answers are deprecated as of 0.13.x and removed in 1.0, here's the updated answer, assuming that printAction is the task that compile should depend on:

    (Compile / compile) := ((Compile / compile) dependsOn printAction).value

    0 讨论(0)
提交回复
热议问题