如何引用生成的源文件? [英] How to refer to generated source files?

查看:150
本文介绍了如何引用生成的源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用sourceGenerators sbt任务生成的文件?我知道源文件存放在target/scala-<version>/src_managed/path/to/File.Scala中,但是我不知道如何在我的项目中引用它.有什么想法吗?

How do I refer to files generated by a sourceGenerators sbt task? I know that the source file is deposited to target/scala-<version>/src_managed/path/to/File.Scala but I don't know how to reference it in my project. Any ideas?

/server
  /app
    /models
      Driver.scala (can't use MyGeneratedCode here)
  /project
    /src/main/scala/
      ModelGenerator.scala  (the code used to create MyGeneratedCode.scala)
    /target/scala-2.12
      /classes
      /src_managed
        /main/generated/
          MyGeneratedCode.scala

推荐答案

生成文件的作用与任何普通的 Scala 文件相同,但位于精美的文件夹中,因此git (或其他任何VCS).
与任何 Scala 文件一样,该文件的开头应带有package whatever语句,以后您可以从该语句中导入类(import whatever._).通常,程序包名称与目录结构匹配,但不必(特别是因为它只是生成的文件).

The generate file works as any normal Scala file but on a fancy folder so it is not tracked by git (or any other VCS).
As any Scala file, it should had a package whatever statement at the beginning, from which you can latter import your classes (import whatever._). Usually the package name matches the directory structure, but they do not have to (specially since it is just generated file).

如果没有软件包,则其所有成员可能都驻留在__root__软件包上.但这可能会引起麻烦,我建议编辑您的生成器以添加一个package语句.

If it does not have a package, all its members probably resides on the __root__ package. But that probably would cause troubles, I would suggest editing your generator to add a package statement instead.

这篇关于如何引用生成的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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