在 sbt 中手动调用 sourceGenerators [英] Call sourceGenerators manually in sbt

查看:29
本文介绍了在 sbt 中手动调用 sourceGenerators的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sourceGenerators in Compile 生成一些 Scala 源文件到 target\scala-2.10\src_managed.当我运行 sbt compile 时,源代码与 src\main\scala 下的常规代码一起生成和编译.

I am using sourceGenerators in Compile to generate some Scala source files to target\scala-2.10\src_managed. When I run sbt compilethe sources are generated and compiled along with the regular code under src\main\scala.

但是如果我想单独生成源代码而不编译怎么办?我正在寻找的是这个流程:

But what if I want to generate the sources separately without compiling? What I am looking for is this flow:

  1. 调用任务生成源代码
  2. 在我的常规资源中使用生成的 IDE 帮助源
  3. 编译一切

如何实现?

推荐答案

更新

如果我现在猜对了,您希望能够单独调用源生成器.为此,您可以简单地在 /build.sbt/project/Project.scala 文件中的某处定义这样的自定义任务:

If I got you correct now, you want to be able to call the source generators seperately. For that you can simply define a custom task like this somewhere in your /build.sbt or /project/Project.scala file:

val generateSources = taskKey[List[File]]("generate sources")

generateSources <<= 
  (sourceGenerators in Compile) { _.join.map(_.flatten.toList) }

然后您可以像这样从 sbt 控制台手动调用生成器:

you can then call the generator manually from the sbt console like this:

> generateSources
[success] Total time: 0 s, completed 07.04.2014 13:42:41

旁注:但是,如果您唯一需要的就是获得 IDE 支持,我建议您设置您的 IDE 以生成源代码.

Side Note: I would however reccomend to setup your IDE to generate the sources if the only thing you need them for is to get IDE support.

旧答案以供将来参考

从非生成类或对象使用生成的类或对象不需要做任何特殊的事情.

You don't need to do anything special to use a generated class or object from a non-generated class or object.

在您的 /build.sbt/project/Project.scala 文件中,您定义源生成器:

In your /build.sbt or /project/Project.scala file you define the source generator:

sourceGenerators in Compile <+= sourceManaged in Compile map { dir =>
  val file = dir / "A.scala"
  IO.write(file, "class A(val name: String)")
  Seq(file)
}

然后您编写一些代码,在/src/main/scala/B.scala 中创建类 A 的实例:

Then you write some code which creates an instance of class A in/src/main/scala/B.scala:

object B extends App {
  val a = new A("It works")
  println(a.name)
}

如果你从 sbt 编译这段代码,它会在编译时考虑生成和非生成代码:

If you compile this code from sbt, it will consider both generated and non-generated code at compilation:

> run
[info] Compiling 2 scala sources to <...>
[info] Running B
It works
[success] Total time: 0 s, completed 07.04.2014 13:15:47

这篇关于在 sbt 中手动调用 sourceGenerators的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆