我想我正在做类似你在以下项目中的第一部分的事情:我的模块gen相当于你的模块A,我的模块核心相当于你的模块B。未经测试,结构大致如下:
// taking inspiration from
// http://stackoverflow.com/questions/11509843/
lazy val ugenGenerator = TaskKey[Seq[File]]("ugen-generate", "Generate UGen class files")
lazy val gen = Project(id = "gen", base = file("gen")) ...
lazy val core = Project(id = "core", base = file("core"))
.settings(
sourceGenerators in Compile <+= ugenGenerator in Compile,
ugenGenerator in Compile := {
val src = (sourceManaged in Compile ).value
val cp = (dependencyClasspath in Runtime in gen ).value
val st = streams.value
runUGenGenerator(description.value, outputDir = src,
cp = cp.files, log = st.log)
}
)
def runUGenGenerator(name: String, outputDir: File, cp: Seq[File],
log: Logger): Seq[File] = {
val mainClass = "my.class.from.Gen"
val tmp = java.io.File.createTempFile("sources", ".txt")
val os = new java.io.FileOutputStream(tmp)
log.info(s"Generating UGen source code in $outputDir for $name")
try {
val outs = CustomOutput(os)
val fOpt = ForkOptions(javaHome = None, outputStrategy = Some(outs), bootJars = cp,
workingDirectory = None, connectInput = false)
val res: Int = Fork.scala(config = fOpt,
arguments = mainClass :: "-d" :: outputDir.getAbsolutePath :: Nil)
if (res != 0) {
sys.error(s"UGen class file generator failed with exit code $res")
}
} finally {
os.close()
}
val sources = scala.io.Source.fromFile(tmp).getLines().map(file).toList
tmp.delete()
sources
}
这在 sbt 0.13 中有效,但我没有时间弄清楚为什么它在 1.x 中不起作用。
sourceGenerators in Compile <+= ugenGenerator in Compile
顺便说一句,如果没有不推荐使用的语法
,我该如何编写?