使用 SBT,我如何指定一个替代项目根目录而不是当前目录来运行主类? [英] With SBT, how do I a specify an alternate project root other than the current directory for running a main class?

查看:18
本文介绍了使用 SBT,我如何指定一个替代项目根目录而不是当前目录来运行主类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常 SBT 在 ./build.sbt./project/Build.scala 中查找构建文件.是否可以指定备用项目根目录,以便我可以构建不在当前工作目录中的项目?我基本上在寻找与 mvn -f/path/to/pom.xml 等效的方法,但文档没有为我提供任何明显的答案.

Normally SBT looks for the build files at ./build.sbt and ./project/Build.scala. Is it possible to specify an alternate project root, so I can build a project not in the current working directory? I'm looking essentially for an equivalent to mvn -f /path/to/pom.xml but the docs have not provided me with any obvious answers.

(我应该注意,我想在运行时而不是编译时执行此操作.本质上,我想使用 sbt run-main 从我的项目中运行任意类,因为我 讨厌手动类路径争吵.由于各种无聊的原因,我可能需要从任意位置执行此操作,因为代码可能需要工作目录不是项目目录,因此使用 cd 可能不做我想做的事.这在 Maven 中是如此微不足道 - 我只是假设,也许不公平,在 SBT 中会有一个简单的等价物)

(I should note that I want to do this at runtime rather than compile time. Essentially, I want to use sbt run-main to run arbitrary classes from my project, since I hate manual classpath wrangling. For various boring reasons I may need to do this from arbitrary location since the code may need the working directory to be something other than the project directory, so using cd might not do what I want. It's so trivial in Maven - I just assumed, perhaps unfairly, that there would be a simple equivalent in SBT)

推荐答案

正如我从 this other answer 中发现的,sbt-start-script 插件是比 sbt run 更好的工具-主.您可以简单地运行 sbt stage,然后在 target/start 处获得一个调用脚本,并解析了类路径.根据文档,需要从构建根目录运行项目间依赖关系才能工作,但对于我的简单用例,这似乎不是问题.

As I discovered from this other answer, the sbt-start-script plugin is a better tool for this than sbt run-main. You can simply run sbt stage and you get an invocation script, with classpaths resolved, at target/start. According to the documentation, it needs to be run from the build root directory for inter-project dependencies to work, but for my simple use cases, this doesn't seem to be a problem.

这篇关于使用 SBT,我如何指定一个替代项目根目录而不是当前目录来运行主类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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