我试图在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.