我在多模块SBT项目中有模块A和模块B.我想为模块B编写一个资源生成器任务,从模块A调用代码。一种方法是从project/
下的模块A中提取所有代码,但这是不可行的,因为模块A很大,我会喜欢把它放在原处(见https://stackoverflow.com/a/47323703/471136)。我如何在SBT中做到这一点?
其次,是否有可能完全摆脱模块B,即我希望模块A的资源生成器任务实际调用来自模块A的代码但模块A是根模块并且不存在于SBT的project
中?
注意:这个问题与此不重复:Defining sbt task that invokes method from project code?因为那个问题解决了将代码移到SBT的项目中,这是我在这里要避免的。
答案 0 :(得分:2)
我想我在做以下项目的第一部分:我的module gen相当于你的模块A,我的module core就是等价物您的模块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
?
答案 1 :(得分:1)
根据上面的@ 0__回答,这是我的简化版本:
/**
* Util to run the main of a sub-module from within SBT
*
* @param cmd The cmd to run with the main class with args (if any)
* @param module The sub-module
*/
def runModuleMain(cmd: String, module: Reference) = Def.task {
val log = streams.value.log
log.info(s"Running $cmd ...")
val classPath = (fullClasspath in Runtime in module).value.files
val opt = ForkOptions(bootJars = classPath, outputStrategy = Some(LoggedOutput(log)))
val res = Fork.scala(config = opt, arguments = cmd.split(' '))
require(res == 0, s"$cmd exited with code $res")
}
然后您可以将其调用为:
resourceGenerators in Compile += Def.taskDyn {
val dest = (resourceManaged in Compile).value
IO.createDirectory(dest)
runModuleMain(
cmd = "com.mycompany.foo.ResourceGen $dest $arg2 $arg3 ...",
module = $referenceToModule // submodule containing com.mycompany.foo.ResourceGen
).taskValue
dest.listFiles()
}
有one more way来执行此操作:
resourceGenerators in Compile += Def.taskDyn {
val dest = (resourceManaged in Compile).value
IO.createDirectory(dest)
Def.task {
val cmd = "com.mycompany.foo.ResourceGen $dest $arg2 $arg3 ..."
(runMain in Compile).toTask(" " + $cmd).value
dest.listFiles()
}
}.taskValue