Defining sbt task that invokes method from project code?

后端 未结 3 1704
醉梦人生
醉梦人生 2020-12-08 11:18

I\'m using SBT to build a scala project. I want to define a very simple task, that when I input generate in sbt:

sbt> generate
3条回答
  •  孤城傲影
    2020-12-08 11:46

    In .sbt format, do:

    lazy val generate = taskKey[Unit]("Generate my file")
    
    fullRunTask(generate, Compile, "my.App")
    

    This is documented at http://www.scala-sbt.org/0.13.2/docs/faq.html, “How can I create a custom run task, in addition to run?”

    Another approach would be:

    lazy val generate = taskKey[Unit]("Generate my file")
    
    generate := (runMain in Compile).toTask(" my.App").value
    

    which works fine in simple cases but isn't as customizable.

    Update: Jacek's advice to use resourceGenerators or sourceGenerators instead is good, if it fits your use case — can't tell from your description whether it does.

提交回复
热议问题