在 sbt 0.13 中使用 .sbt 文件编写多项目构建的惯用方式 [英] Idiomatic way to write multi-project builds with .sbt files in sbt 0.13

查看:28
本文介绍了在 sbt 0.13 中使用 .sbt 文件编写多项目构建的惯用方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说 .sbt 文件在 0.13 中以各种方式进行了改进,现在我可以在其中指定多项目构建.

I hear .sbt files have been improved in various ways in 0.13, and that now I can specify multi-project builds in them.

http://www.scala-sbt.org/0.13.0/docs/Community/ChangeSummary_0.13.0.html#sbt-format-enhancements 提到我们现在可以在 .sbt 文件中定义子项目.我也知道根目录下的多个 .sbt 文件会聚合成一个概念文件.

http://www.scala-sbt.org/0.13.0/docs/Community/ChangeSummary_0.13.0.html#sbt-format-enhancements mentions that we can now define subprojects in a .sbt file. I also know that multiple .sbt files in the root will be aggregated into a single conceptual file.

不过,我真正想要的是不要让十几个子项目 .sbt 文件污染我的根目录.有没有办法可以将子项目 build.sbt 文件放入它们各自的子目录中,在它们之间共享一些公共代码,然后为聚合子项目的整个项目创建一个根 build.sbt?我现在在 .scala 文件中有类似的设置,但如果可能的话,我更愿意使用 .sbt 文件.

What I'd really like, though, is to not pollute my root with a dozen subproject .sbt files. Is there a way I can throw the subproject build.sbt files into their respective subdirectories, keep some common code between them somewhere shared, and then have a root build.sbt for the entire project that aggregates the subprojects? I have a similar setup in .scala files right now but would prefer to use .sbt files if possible.

如果不可能,用 .sbt 文件构建大型多项目构建的正确"方法是什么?

If that isn't possible, what is the "correct" way to construct large multi-project builds with .sbt files?

推荐答案

0.12 应该已经是这样了,你可以将 .sbt 文件放在子项目的基目录中,并在那里进行设置将包含在该项目的范围内.

It should already be the case in 0.12 that you can put .sbt files in the base directory of a subproject and the settings there will be included in that project's scope.

通过在 project/ 中创建一个普通的 .scala 文件,可以在 .sbt 文件之间重用代码.project/ 中的代码可在 .sbt 文件中使用.一个 .sbt 中的定义对其他 .sbt 文件是不可见的,至少在 0.13 中是这样.这主要是一个实现限制,未来版本是否会取消,尚不确定.

Code is reused between .sbt files by creating a normal .scala file in project/. The code in project/ will be available for use in the .sbt files. The definitions in one .sbt are not visible to other .sbt files, at least in 0.13. This is mainly an implementation restriction and it is undetermined whether this will be lifted in future versions.

默认的根项目会聚合所有子项目,包括那些来自subProject/build.sbt中定义的项目.

The default root project will aggregate all subprojects, including those coming from projects defined in subProject/build.sbt.

当前的困难在于明确.例如,根目录中的以下 build.sbt 将在 sub/ 中定义一个子项目.这是一个完整的定义,定义了项目的 ID、基目录等.

The current difficulty is making it explicit. For example, the following build.sbt in the root directory would define a subproject in sub/. This is a full definition, defining the ID, base directory, etc... for the project.

<root>/build.sbt

lazy val sub = project

但是,它不能引用 <sub>/build.sbt 中定义的任何内容.(sub/build.sbt 的存在直到 <root>/build.sbt 被编译和评估之后才知道.)因此,要明确定义 sub 聚合的内容,您需要类似以下内容:

However, it cannot reference anything defined in <sub>/build.sbt. (The existence of sub/build.sbt is not known until after <root>/build.sbt is compiled and evaluated.) So, to explicitly define what sub aggregates, you'd need something like:

sub/build.sbt

lazy val sub = project.in(file(".")).aggregates(subSub)
//or: lazy val sub = project in file(".") aggregate subSub

lazy val subSub = project

但是,这重复了 sub 的定义.

However, this duplicates the definition of sub.

一个可能的解决方案是让根定义只是一个参考,例如:

A possible solution going forward is to make the root definition just a reference, like:

<root>/build.sbt

lazy val sub = LocalProject("sub")

这篇关于在 sbt 0.13 中使用 .sbt 文件编写多项目构建的惯用方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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