假设我有一个现有的(旧的)代码库,我想要演示如何使用SBT编译它的最小侵入方式。我想以这样的方式完成它,即包含代码库的根目录的文件结构完全不受影响。这意味着project/Build.scala
将需要在实际项目之外。另外,请记住,由于现有项目具有非标准布局并且在非托管依赖项上使用很多,因此需要完成相当多的配置(例如设置unmanagedSourceDirectories,unmanagedJars等)。
考虑下面的示例布局:
/opt/workspace
|
+- existing-codebase
| |
| +- module1
| | |
| | +- src
| |
| +- module2
| |
| +- src
|
+- sbt-demo-build
|
+- project
|
+- Build.scala
另外,假设我有以下环境变量集:
CODE_HOME=/opt/workspace/existing-codebase
那么,需要进入Build.scala
以表明构建基础(buildBase
?)是/opt/workspace/existing-codebase
而不是/opt/workspace/sbt-demo-build
我尝试设置baseDirectory
密钥,但SBT失败了:
java.lang.AssertionError:断言失败:目录/ opt / workspace / existing-codebase / module1未包含在构建根目录/ opt / workspace / sbt-demo-build
中
这是我在项目中使用的DemoBuild
:
import java.nio.file.{Paths, Files}
import sbt._
import Keys._
object DemoBuild extends Build {
val codeHome = Paths.get(sys.env("CODE_HOME")).toAbsolutePath.
toRealPath().normalize().toFile
...
}
我能想到的最好的类比是basedir
元素中Ant的project
属性 - SBT中是否存在这个概念(或者它是否未正确曝光)?
在挖了一下后,我发现了一个名为xsbti.AppConfiguration
的东西,它似乎对应于一个名为sbt.Keys.appConfiguration
的设置。我试图覆盖使用:
appConfiguration ~= { cfg =>
new AppConfiguration {
def arguments(): Array[String] = cfg.arguments()
def baseDirectory(): File = codeHome
def provider(): AppProvider = cfg.provider()
}
}
但是得到了相同的AssertionError。然后,我找到了一些可能会有帮助的official docs,所以我会在下一步中查看这些
答案 0 :(得分:4)
使用/opt/workspace
作为SBT项目的项目目录。按照您的意图使用它/opt/workspace/sbt-demo-build
。
在build.sbt
中使用以下构建定义,定义当前项目目录之外的两个SBT模块。
lazy val root = project in file(".") aggregate (module1, module2)
lazy val module1 = ProjectRef(file("../existing-codebase/module1"), "module1")
lazy val module2 = ProjectRef(file("../existing-codebase/module2"), "module2")
它假定module1
和module2
是SBT项目,root
项目聚合它们,以便在root
will also run it on the aggregated projects中运行命令。
在Unix上,使用ln -s
的项目符号链接在构建根目录之外。
jacek:~/sandbox/so/andrey-workspace/sbt-demo-build
$ ln -s ../existing-codebase/module1 .
jacek:~/sandbox/so/andrey-workspace/sbt-demo-build
$ ln -s ../existing-codebase/module2 .
jacek:~/sandbox/so/andrey-workspace/sbt-demo-build
$ tree
.
├── build.sbt
├── module1 -> ../existing-codebase/module1
└── module2 -> ../existing-codebase/module2
2 directories, 1 file
使用以下build.sbt
定义项目。
jacek:~/sandbox/so/andrey-workspace/sbt-demo-build
$ cat build.sbt
lazy val root = project in file(".") aggregate (module1, module2)
lazy val module1 = project
lazy val module2 = project
在SBT控制台中,projects
,project
和run
运作良好:
jacek:~/sandbox/so/andrey-workspace/sbt-demo-build
$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to root (in build file:/Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/)
[root]> projects
[info] In file:/Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/
[info] module1
[info] module2
[info] * root
[root]> module1/run
[info] Running Hello
Hello from module1
[success] Total time: 1 s, completed Jan 11, 2014 9:40:03 PM
[root]> project module1
[info] Set current project to module1 (in build file:/Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/)
[module1]> run
[info] Running Hello
Hello from module1
[success] Total time: 0 s, completed Jan 11, 2014 9:40:20 PM
[module1]> project root
[info] Set current project to root (in build file:/Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/)
[root]> show baseDirectory
[info] module1/*:baseDirectory
[info] /Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/module1
[info] module2/*:baseDirectory
[info] /Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build/module2
[info] root/*:baseDirectory
[info] /Users/jacek/sandbox/so/andrey-workspace/sbt-demo-build