10

我在将项目迁移到 SBT 时遇到问题0.13

出于某种原因,来自 SBT 文档的Generate sources的片段对我不起作用。

不幸的是,简单的.sbt构建定义和 Scala 构建定义都不起作用。项目定义取自文档:

name := "sbt-test"

sourceGenerators in Compile += Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}

编译项目定义时,编译器会报类型错误:

~/P/sbt-test ▶ sbt
[info] Loading global plugins from /Users/phearnot/.sbt/0.13/plugins
[info] Loading project definition from /Users/phearnot/Projects/sbt-test/project
/Users/phearnot/Projects/sbt-test/build.sbt:3: error: No implicit for Append.Value[Seq[sbt.Task[Seq[java.io.File]]], sbt.std.FullInstance.M[Seq[java.io.File]]] found,
  so sbt.std.FullInstance.M[Seq[java.io.File]] cannot be appended to Seq[sbt.Task[Seq[java.io.File]]]
sourceGenerators in Compile += Def.task {
                            ^
[error] Type error in expression
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? 

更新:既然AlexIv已经指出了我的 SBT 文件定义中的问题,我想知道将它移动到 Scala 构建定义中的正确方法是什么?

4

3 回答 3

8

Def.task将您的 build.sbt(从 gi​​st)更改为Def.task[Seq[File]]或仅将task[Seq[File]]原因Def自动导入到build.sbt

name := "sbt-test"

sourceGenerators in Compile += task[Seq[File]] {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}

然后调用compilesbt。Test.scala将产生于./target/scala-2.10/src_managed/main/demo/Test.scala

于 2013-09-23T08:11:31.317 回答
0

我的 sbt 版本是 0.13.8,它适用于我。

于 2016-03-17T15:26:59.507 回答
0

使用<+=代替+=

name := "sbt-test"

sourceGenerators in Compile <+= Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}
于 2015-10-09T17:29:49.083 回答