SBT generate code using project defined generator

后端 未结 2 2039
青春惊慌失措
青春惊慌失措 2020-12-10 03:24

I\'d like to compile a project which contains a java source generator and then compile the generated code within a single project. I.e: compile Generator.scala, run Generato

2条回答
  •  無奈伤痛
    2020-12-10 03:51

    The project description is compiled when loading it. There is no way to directly call the new code generated at runtime. Unless I guess using some sort of reflection making sure there is no forking of the JVM and somehow having those classes loaded into the classloader.

    The only way I can think of doing it is making a project inside your project definition.

    root
     - src
     - project/
       - Build.scala // normal project definition
       - project/
         - Build.scala // inner most
    

    In the inner most project definition you may be able to define the outer src as the src folder. That will get you a compiled version of the Generator available for the real project. Then in the normal project definition add an import to the Generator and use it as you were doing.

    I'm pretty sure the inner most project will only be loaded and compiled once. You will need to have sbt reload the project definition if you make changes to the Generator. Exiting and reopening is the simplest/dumbest way of doing it but it may help testing. Lookup later smarter ways of reloading if it works.

提交回复
热议问题