问题描述
我想在我的源代码中调用 sbt update
来更新多个 sbt 项目.在 shell 中,这很容易:
I would like to call sbt update
within my source-code, to update multiple sbt projects. In the shell this is easy:
cd /path/to/project && sbt update
但是如果我在我的代码中使用 scala.sys.process
,它不会记住 cd
因此 sbt
在错误的目录.代码如下:
But if I use scala.sys.process
within my code, it won't remember the cd
therefore sbt
is called in the wrong directory. Code like this:
import scala.sys.process._
("cd /path/to/project" #&& "sbt update").!!
而且我没有在文档中找到任何通过控制台设置 sbt 项目路径的可能性.如果这样的事情有效,那就太好了:
And I didn't find in the documentation any possibility to set sbt's project path via console. It would be nice if something like this works:
"sbt -projectPath /path/to/project update".!!
如果这样的事情是可能的,这将为我省去很多麻烦!(特别是它在 UNIX 和 Windows 上运行.)
If something like that is possible, this would save me a lot of mess! (Especially that it runs on UNIX and Windows.)
推荐答案
在 Process
对象上使用 ProcessBuilder
工厂方法之一:
Use one of the ProcessBuilder
factory methods on the Process
object:
sys.process.Process(Seq("sbt","update"), new java.io.File("/path/to/project")).!!
有关更多文档,请参阅 scaladoc 文件对于 sys.process 包.不幸的是,它没有提到当前工作目录"参数,但它们在对象 Process
的文档中.
For more documentation, see the scaladoc file for the sys.process package. Unfortunately, it does not mention the 'current working directory' arguments, but they are in the documentation of object Process
.
这篇关于执行shell进程时设置当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!