gpt4 book ai didi

sbt - "buildBase"可以指向 SBT 中当前项目之外的目录吗?

转载 作者:行者123 更新时间:2023-12-04 20:03:21 29 4
gpt4 key购买 nike

假设我有一个现有的(旧的)代码库,并且我想要演示如何使用 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: assertion failed: Directory /opt/workspace/existing-codebase/module1 is not contained in build root /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
...
}

我能想到的最好的比喻是 Ant 的 basedir 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这可能会有所帮助,所以我将研究这些作为下一步

最佳答案

选项1
使用/opt/workspace作为 SBT 项目的项目目录。使用它,因为你打算使用 /opt/workspace/sbt-demo-build .
选项 2
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")
它假定 module1module2是 SBT 项目和 root项目聚合它们,因此在 root 中运行命令 will also run it on the aggregated projects .
选项 3
在 Unix 上,使用 ln -s 符号链接(symbolic link)项目在构建根目录之外。
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 , projectrun完美地工作:
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

关于sbt - "buildBase"可以指向 SBT 中当前项目之外的目录吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21029916/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com