如何在sbt多项目构建中获取子项目路径 [英] how to get the sub project path in sbt multi project build

查看:40
本文介绍了如何在sbt多项目构建中获取子项目路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the location of sub project in multi-project build in sbt. But I am able to get only the root project directory.

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")没有返回路径?

As a work around , I have added the sub_project folder using the rootLocation. But why the file("sub_project") is not returning the path ?

推荐答案

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

If you define your subproject like this

lazy val subProject = project in file("sub_project") // ...

然后您可以使用范围内的 baseDirectory 设置获取其路径:

then you can get its path using the scoped baseDirectory setting:

baseDirectory.in(subProject).value.getAbsolutePath

或者在 sbt 控制台中:

Or in the sbt console:

> show subProject/baseDirectory

关于你的代码的问题(除了你在输出中混淆了根和子项目)是相对路径的使用.关于 Paths 的 Sbt 文档明确指出

About the problem with your code (beside that you mixed up root and sub-project in the output) is the usage of relative paths. Sbt documentation on Paths explicitly says

相对文件应该在定义项目的基目录时使用,它们将被正确解析.

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

在其他地方,文件应该是绝对的,或者是从绝对基础文件构建的.baseDirectory 设置根据范围定义构建或项目的基目录.

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.

这篇关于如何在sbt多项目构建中获取子项目路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆