SBT 0.10 和 IDEA 初学者指南

发布于 2024-11-29 09:32:50 字数 603 浏览 2 评论 0原文

我是 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 the build.sbt file? This means IDEA doesn't recognize object 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴梦长久 2024-12-06 09:32:50

这对我有用:

首先让 sbt 和 gen-idea 插件运行...

  1. 下载 sbt-launch.jar 并创建用于启动它的脚本,如 SBT Github wiki
  2. 为您的新项目创建一个目录,例如(在 Linux 上)~/myCode/myNewProject 并更改到该目录
  3. 运行 sbt 命令。这应该下载 scala 库并创建“项目”和“目标”目录。
  4. 更改为“项目”目录。
  5. 在此目录中使用以下行创建一个新文件“build.sbt”,如 sbt 中所述-idea插件Github wiki

    解析器+=“sbt-idea-repo”位于“http://mpeltonen.github.com/maven/”
    
    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.0.0")
    
  6. 更改回您的主项目目录,例如 ~/myCode/myNewProject。运行 sbt。它应该下载 gen-idea 插件。

  7. 从 sbt 控制台(现在应该正在运行)运行 gen-idea 命令。它应该创建 IDEA 项目目录。对我来说,它也发出了大量警告。

现在让 IDEA SBT 控制台插件运行起来...

  1. 打开 IDEA 并从插件管理器安装“SBT”插件,然后重新启动 IDEA。 (注意这是 IDEA 插件,而不是上面描述的 sbt 插件。)
    配置 SBT 插件按照其 wiki 上的描述(运行配置、sbt-的位置)启动.jar 等)。
  2. 在IDEA中打开刚刚生成的IDEA项目。
  3. 将您的代码和其他内容放入预期的默认目录中,如 sbt wiki 的“目录布局”< /a>.您需要自己创建这些目录 - sbt 不会自动创建它们。 “src”和“test”目录应与 sbt 创建的“project”和“target”目录处于同一级别。
  4. 创建一个新的“build.sbt”文件并将其放入 ~/myCode/myProject(或任何您所称的名称)中。因为我只是在弄清楚 sbt,所以到目前为止我的很简单 - 只是指定 scalatest 作为依赖项并使用 Scala 2.9:

    名称 := "myProject"
    
    版本:=“0.1”
    
    组织:=“我”
    
    库依赖项+=“org.scalatest”%“scalatest_2.9.0”%“1.6.1”
    
    scala版本 := "2.9.0"
    
  5. 在 IDEA 屏幕底部的 SBT 控制台中输入 reload 命令。它应该为您下载 scalatest 和 Scala 2.9。也许您还需要运行“更新”。

This worked for me:

First get sbt and the gen-idea plugin going...

  1. Download the sbt-launch.jar and create the script for launching it as described on the SBT Github wiki.
  2. Create a directory for your new project, such as (on linux) ~/myCode/myNewProject and change to that directory
  3. Run sbt command. This should download the scala libraries and create a 'project' and 'target' directories.
  4. Change to the 'project' directory.
  5. Create a new file 'build.sbt' in this directory with the following lines, as described on the sbt-idea plugin Github wiki:

    resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
    
    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.0.0")
    
  6. Change back to your main project directory such as ~/myCode/myNewProject. Run sbt. It should download the gen-idea plugin.

  7. From the sbt console (which should be running now), run the 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...

  1. Open IDEA and install the "SBT" plugin from the plugin manager and restart IDEA. (Note this is the IDEA plugin, not the sbt plugin described above.)
    Configure the SBT plugin as described on its wiki (run configurations, location of sbt-launch.jar etc).
  2. Open the freshly generated IDEA project in IDEA.
  3. Put your code and other things in the expected default directories as described on the sbt wiki under 'Directory Layout'. You need to create these directories yourself - sbt doesn't create them automatically. The 'src' and 'test' directories should be at the same level as the 'project' and 'target' directories that sbt created.
  4. 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:

    name := "myProject"
    
    version := "0.1"
    
    organization := "me"
    
    libraryDependencies += "org.scalatest" % "scalatest_2.9.0" % "1.6.1"
    
    scalaVersion := "2.9.0"
    
  5. 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.

笨死的猪 2024-12-06 09:32:50

我写了一个非常快速的指南 关于它。它旨在成为 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.

哭了丶谁疼 2024-12-06 09:32:50

我通常将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文