gpt4 book ai didi

scala - 如何在sbt多项目构建中获取子项目路径

转载 作者:行者123 更新时间:2023-12-04 18:02:49 24 4
gpt4 key购买 nike

我试图在 sbt 的多项目构建中获取子项目的位置。但我只能获得根项目目录。

lazy val copyToResources = taskKey[Unit]("copies the assembly jar.")
private val rootLocation: File = file(".").getAbsoluteFile
private val subProjectLocation: File = file("sub_project").getAbsoluteFile.getParentFile
lazy val settings = Seq(copyToResources := {
val absPath = subProjectLocation.getAbsolutePath
println(s"rootLocation:$subProjectLocation $absPath, sub-proj-location: ${rootLocation.getAbsolutePath}")
})

输出:
 rootLocation:/home/user/projects/workarea/repo /home/vdinakaran/projects/workarea/repo, sub-proj-location: /home/vdinakaran/projects/workarea/repo
rootLocation:/home/user/projects/workarea/repo /home/vdinakaran/projects/workarea/repo, sub-proj-location: /home/vdinakaran/projects/workarea/repo

目录结构:
repo
|-- sub_project

作为解决方法,我使用 rootLocation 添加了 sub_project 文件夹。但是为什么文件(“sub_project”)没有返回路径?

最佳答案

如果你这样定义你的子项目

lazy val subProject = project in file("sub_project") // ...
然后你可以使用作用域 baseDirectory 来获取它的路径环境:
baseDirectory.in(subProject).value.getAbsolutePath
或者在 sbt 控制台中:
> show subProject/baseDirectory
关于你的代码的问题(除了你在输出中混淆了根和子项目)是相对路径的使用。关于 Paths 的 Sbt 文档明确地说

Relative files should only be used when defining the base directory of a Project, where they will be resolved properly.

Elsewhere, files should be absolute or be built up from an absolute base File. The baseDirectory setting defines the base directory of the build or project depending on the scope.

关于scala - 如何在sbt多项目构建中获取子项目路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766783/

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