加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 18:06:28 所属栏目:安全 来源:网络整理
导读:我试图在sbt中的多项目构建中获得子项目的位置.但我只能获得根项目目录. lazy val copyToResources = taskKey[Unit]("copies the assembly jar.")private val rootLocation: File = file(".").getAbsoluteFileprivate val subProjectLocation: File = file("
我试图在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") // ...

然后你可以使用scoped 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读