scala - SBT:编译前为子模块生成代码
问题描述
我对 build.sbt 配置有以下问题。我需要在编译之前生成一些代码。这就是它现在的工作方式。
lazy val rootProject = project.in(file("."))
lazy val rootSourceGenerator = Def.task {
val f: File = (sourceManaged in Compile).value / "com" / "myproject" / "Version.scala"
IO.write(
f,
s"""package com.myproject
|
|object Version {
| some code ...
|}
|""".stripMargin
)
Seq(f)
}
inConfig(Compile)(
Seq(
sourceGenerators += rootSourceGenerator
))
现在我需要为一个新的子模块做同样的事情。
lazy val rootProject = project.in(file(".")).dependsOn(submodule)
lazy val submodule = project.in(file("submodule"))
lazy val submoduleSourceGenerator = Def.task {
val f: File = (sourceManaged in (submodule, Compile)).value / "com" / "myproject" / "SubmoduleVersion.scala"
IO.write(
f,
s"""package com.myproject
|
|object SubmoduleVersion {
| some code ...
|}
|""".stripMargin
)
Seq(f)
}
inConfig(submodule / Compile)(
Seq(
sourceGenerators += submoduleSourceGenerator
))
并且inConfig(submodule / Compile)
不起作用。错误是关于/
. 任何建议如何解决这个问题?
解决方案
有多种解决方案,但我认为最干净的是遵循。
使用以下内容创建AutoPluginproject/GenerateVersion.scala
import sbt.Keys._
import sbt._
object GenerateVersion extends AutoPlugin {
override def trigger = noTrigger
override def projectSettings: Seq[Def.Setting[_]] = {
Seq(
sourceGenerators in Compile += Def.task {
val f: File =
(sourceManaged in Compile).value / "com" / "myproject" / "Version.scala"
IO.write(
f,
s"""package com.myproject
|
|object Version {
|}
|""".stripMargin
)
Seq(f)
}.taskValue
)
}
}
GenerateVersion
为所有需要Version.scala
生成的项目/子模块启用新创建的插件。可以按照以下方式完成build.sbt
lazy val sub = project
.in(file("sub"))
.enablePlugins(GenerateVersion)
lazy val root = project
.in(file("."))
.enablePlugins(GenerateVersion)
.aggregate(sub)
aggregate(sub)
sub
在触发根任务时添加以运行模块中的任务。例如,sbt compile
将同时运行sbt "root/compile" "sub/compile"
这个解决方案更容易以 SBT 插件的形式在多个 SBT 项目中共享。
另外,您可能对sbt-builtinfo插件感兴趣
推荐阅读
- java - 条件查询返回 []
- mysql - 如何将表的一列从一个特定数据库迁移到另一个数据库列?
- c# - 将服务注入另一个服务是不好的做法吗?
- javascript - JSON 按名称分组,然后显示最新记录
- html - 具有溢出自动的表隐藏具有绝对位置的内容
- mongodb - MongoDB - 返回列表,项目用双引号括起来
- mysql - 使用 MySQL 中的 EXISTING 计数列创建更新的计数列
- javascript - 如何在 JavaScript 中组合不同的条件?
- python - 如何正确地将 JSON 数组传递给 Django 中的 HTML 模板?
- javascript - Selectpicker 不能与表单转发器一起使用