SBT 0.10 和 IDEA 初学者指南
我是 SBT 新手,不确定如何开始项目。有人可以向我指出创建 Hello World 类型项目的初学者指南,或者给我一些线索吗?
我首选的 IDE 是 IDEA。我已经根据 IDEA 上的说明运行了 sbt-idea
插件页面。目前我有点困惑,因为
- 没有创建源文件夹 - 我应该在哪里/如何创建它们以及 SBT 如何知道在哪里查找?
- 当我已经将
scalaVersion := "2.9.0"
放入build.sbt
文件中时,为什么它尝试使用 Scala 2.8.1?这意味着 IDEA 无法识别object HelloWorld extends App {}
。 - 上面插件页面上的说明建议更改“运行配置(包括默认运行配置)”的启动前选项。对于列出的不同事物,有 13 种不同的默认配置 - 要更改哪一项?我应该创建一个新的吗?这些默认配置仅适用于该项目还是会对我所有其他不使用 SBT 的项目产生不利影响?
谢谢。
I'm new to SBT and am unsure how to get a project started. Can someone point me to a beginner's guide to creating a Hello World type project, or give me some clues?
My preferred IDE is IDEA. I have run sbt-idea
according to the instruction on the IDEA Plugins page. At the moment I'm a bit confused because
- there are no source folders created - where / how am I supposed to create them and how will SBT know where to look?
- why is it trying to use Scala 2.8.1, when I have already put
scalaVersion := "2.9.0"
in thebuild.sbt
file? This means IDEA doesn't recognizeobject HelloWorld extends App {}
. - the instructions on the plugins page above suggest changing the Before Launch options of "a Run Configuration (including the Default Run Configuration)". There are 13 different default configurations for different things listed - which one to change? Should I be creating a new one? Are these default configurations just for this project or will it adversely affect all my other projects that don't use SBT?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
首先让 sbt 和 gen-idea 插件运行...
在此目录中使用以下行创建一个新文件“build.sbt”,如 sbt 中所述-idea插件Github wiki:
更改回您的主项目目录,例如 ~/myCode/myNewProject。运行 sbt。它应该下载 gen-idea 插件。
现在让 IDEA SBT 控制台插件运行起来...
配置 SBT 插件按照其 wiki 上的描述(运行配置、sbt-的位置)启动.jar 等)。
创建一个新的“build.sbt”文件并将其放入 ~/myCode/myProject(或任何您所称的名称)中。因为我只是在弄清楚 sbt,所以到目前为止我的很简单 - 只是指定 scalatest 作为依赖项并使用 Scala 2.9:
在 IDEA 屏幕底部的 SBT 控制台中输入
reload
命令。它应该为您下载 scalatest 和 Scala 2.9。也许您还需要运行“更新”。This worked for me:
First get sbt and the gen-idea plugin going...
sbt
command. This should download the scala libraries and create a 'project' and 'target' directories.Create a new file 'build.sbt' in this directory with the following lines, as described on the sbt-idea plugin Github wiki:
Change back to your main project directory such as ~/myCode/myNewProject. Run
sbt
. It should download the gen-idea plugin.gen-idea
command. It should create the IDEA project directories. For me, it also emits copious warnings.Now get the IDEA SBT console plugin going...
Configure the SBT plugin as described on its wiki (run configurations, location of sbt-launch.jar etc).
Make up a new 'build.sbt' file and put it in ~/myCode/myProject (or whatever you called it). Since I am just figuring out sbt, mine is simple so far - just nominates scalatest as a dependency and uses Scala 2.9:
Enter the
reload
command in the SBT console at the bottom of the IDEA screen. It should download scalatest and Scala 2.9 for you. Maybe you need to run 'update' too.我写了一个非常快速的指南 关于它。它不旨在成为 SBT 指南 - 没有办法击败 SBT Wiki 为此。这也是毫无意义的,因为人们只能为维基本身做出贡献。
但是,我认为我的快速指南将帮助您按照您的意愿启动并运行。
至于目录创建,我得到的答案是 SBT 希望 IDE 能够处理这个问题——我不太喜欢这种态度,但插件可以完成这项工作。你会看到我安装了 sbt eclipse 插件,这样它就会为我做这件事,即使我自己使用 IDEA(当我使用 IDE 时)。
另请注意,理想情况下,您可以同时使用您提到的 SBT 的 IDEA 插件和 IDEA 的 SBT 插件。有关插件列表,请参阅此处。
除非 IDEA 插件已经发展了很多,否则您确实需要从 SBT 本身生成 IDEA 配置 - IDEA 不会“读取”您的配置。这就是 IDEA 的 SBT 插件的作用。安装它,然后
sbt gen-idea
。我希望这能解决你提到的问题。但请注意,您用于编译项目的 Scala 版本和 SBT 本身使用的 Scala 版本确实不同。这不是问题,正如预期的那样。从你的问题中我不确定你提到的2.8.1版本是SBT使用的版本,还是IDEA使用的版本——或者甚至是用于编译你的项目的版本,表明有些东西不起作用。
无论如何,你把你提到的例子放在哪里了?您应该遵循 maven 风格的目录层次结构,这意味着将其放在 src/main/scala/ 上,如果您也遵循 Java 约定,则可能将其放在与包相关的子目录中。
并尝试使用 sbt 进行编译,以确保其正常工作,然后再继续使用 IDEA。
I wrote a very quick guide about it. It is not intended to be an SBT guide -- there's no way to beat the SBT Wiki for that. It would be pointless too, since one can just contribute to the wiki itself.
But, I think my very quick guide will get you up and running as you wish.
As for directory creation, the answer I got was that SBT expects the IDE to handle that -- I don't really like that attitude, but a plugin can do the job. You'll see I install the sbt eclipse plugin just so it will do it for me, even though I use IDEA myself (when I use an IDE).
Also, note that, ideally, you use both the IDEA plugin for SBT that you mentioned, and the SBT plugin for IDEA. See here for the list of plugins.
Unless the IDEA plugin has evolved a lot, you really need to generate an IDEA configuration from SBT iself -- IDEA won't "read" your configuration. That's what the SBT plugin for IDEA does. Install it, and
sbt gen-idea
. I expect that will solve the problem you mentioned.Note, however, that the version of Scala you use to compile your project and the version of Scala that SBT uses for itself are different indeed. This is not a problem, it is as expected. I'm not sure from your question if the 2.8.1 version you mentioned is the one used by SBT, or one used by IDEA -- or even one used to compile your project, indicating that something is not working.
Where did you put the example you mentioned anyway? You should follow maven-style directory hierarchy, which means putting it on
src/main/scala/
, and possibly a subdirectory of that related to the package, if you follow Java convention as well.And try to compile with sbt, to make sure that is working, before proceeding to IDEA.
我通常将 src 放入文件夹“src/test/scala”和“src/main/scala”中。 sbt-idea 会在 idea 中添加此文件夹 ase source/test 文件夹。
sbt本身需要Scala 2.8.1,但这个版本与你的代码版本无关。如果您尝试编译某些源代码,sbt 将下载 Scala 2.9.0。
I normally put the src into a folder "src/test/scala" and "src/main/scala". sbt-idea will add this folders ase source/test folder in idea.
sbt itself needs Scala 2.8.1 but this version has nothing to do with the version of your code. If you try to compile some source sbt will download Scala 2.9.0.