[英]Scala sbt multi/module projects: how to deal with relative paths?
This could be a silly question, but I haven't found a solution to this problem. 这可能是一个愚蠢的问题,但是我还没有找到解决该问题的方法。 I have a multi-module project with sub-projects (sub1, sub2).
我有一个带有子项目(sub1,sub2)的多模块项目。 Here's a figure:
这是一个图:
main
+-- sub1
+-- src/main/resources/cfg.properties
+-- sub2
I have a cfg file in sub1 and I load it from a class defined in sub1
path using File("./src/main/resources/cfg.properties")
and it works fine. 我在sub1中有一个cfg文件,我可以使用
File("./src/main/resources/cfg.properties")
在sub1
路径中定义的类中加载它,并且效果很好。 If I move to sub2
path and I run the projects, I get a 'file not found' when accessing the class defined in sub1 that loads the file because the path is relative. 如果我移到
sub2
路径并运行项目,则由于访问路径是相对的,因此在访问sub1中定义的类(该类加载文件)时访问“找不到文件”。
What is the best approach to solve this issue? 解决此问题的最佳方法是什么? I'd say that absolute paths could be a solution, but they are not portable to other positions in the file system nor other machines.
我会说绝对路径可能是一个解决方案,但是它们不能移植到文件系统或其他计算机中的其他位置。
I suppose there are best practices to deal with this. 我认为有最佳实践可以解决这个问题。
Hope this helps: 希望这可以帮助:
Sub1 is the name of a sub-project Sub1是子项目的名称
val commonResource = (resourceDirectory in sub1 in Compile).value
fileCopy(baseDir = commonResource, source = "application-ref.conf", target = "application.conf", logMessage = "Akka AppConf")
def fileCopy(baseDir: File, source: String, target: String, logMessage: String)(implicit log: Logger) = {
val sourceFile = baseDir / source
val targetFile = baseDir / target
IO.copyFile(sourceFile, targetFile)
log.info(s"Copied: $logMessage: [$sourceFile -> $targetFile]")
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.