scala 如何使用 IntelliJ Idea 创建 SBT 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4250318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:35:15  来源:igfitidea点击:

How to create SBT project with IntelliJ Idea?

scalaintellij-idealiftsbt

提问by ksemeks

I just got started with Scala/LiftWeb/Sbt developing, and I'd like to import a Sbt project in IntelliJ Idea. Actually, I managed to import my project in two different ways:

我刚开始使用 Scala/LiftWeb/Sbt 开发,我想在 IntelliJ Idea 中导入一个 Sbt 项目。实际上,我设法以两种不同的方式导入我的项目:

1) with Maven. I created a Maven project, and of top of that I created a Sbt project, which I then imported in IntelliJ. I could then easily start, stop the jetty server, and do other stuff. But that's not what I want. I want to do the same stuff, just Maven-free. That lead me to

1) 与 Maven。我创建了一个 Maven 项目,最重要的是我创建了一个 Sbt 项目,然后我将其导入到 IntelliJ 中。然后我可以轻松地启动、停止码头服务器,并做其他事情。但这不是我想要的。我想做同样的事情,只是没有 Maven。这导致我

2) with Eclipse. So, I created a new Sbt project (with a little script I wrote, configuring the Sbt project to be a WebProject). I used then the sbt-eclipsify plugin to 'convert' the project for Eclipse, which I then imported in IntelliJ (existing source -> eclipse). But the problems started here: I cannot get the IntelliJ Sbt plugin to work.

2) 与 Eclipse。所以,我创建了一个新的 Sbt 项目(用我写的一个小脚本,将 Sbt 项目配置为一个 WebProject)。然后我使用 sbt-eclipsify 插件来“转换”Eclipse 的项目,然后我将其导入 IntelliJ(现有源 -> eclipse)。但问题从这里开始:我无法让 IntelliJ Sbt 插件工作。

Can anyone help me with this?

谁能帮我这个?

回答by Steve Gury

There are three basic ways how to create a project - modern versions of IntelliJ can import sbt project out of the box, otherwise you can either use sbt plugin to generate IntelliJ project, or use IntelliJ Scala plugin to create sbt project. Basic features work out of the box using both solutions, some complex builds can have problems, so try other tools to see if it works there.

创建项目有三种基本方式——现代版本的 IntelliJ 可以开箱即用地导入 sbt 项目,否则您可以使用 sbt 插件生成 IntelliJ 项目,或者使用 IntelliJ Scala 插件创建 sbt 项目。使用这两种解决方案,基本功能开箱即用,一些复杂的构建可能会出现问题,因此请尝试其他工具,看看它是否在那里工作。

IntelliJ

智能

IntelliJ IDEA has become so much better these days. The current version (14.0.2) supports sbt projects out of the box with the Scala plugin. Just install the plugin and you should be able to open up Scala/sbt projects without any troubles.

如今,IntelliJ IDEA 变得更好了。当前版本 (14.0.2) 通过 Scala 插件支持开箱即用的 sbt 项目。只需安装该插件,您就应该能够毫无困难地打开 Scala/sbt 项目。

Scala plugin under Plugins in Preferences

Preferences 中 Plugins 下的 Scala 插件

With the plugin, just point at a sbt project and IDEA is going to offer you a wizard to open that kind of project.

使用该插件,只需指向一个 sbt 项目,IDEA 就会为您提供一个向导来打开此类项目。

Import sbt project

导入sbt项目

IntelliJ Scala Plugin

IntelliJ Scala 插件

IntelliJ plugin can be found here http://confluence.jetbrains.com/display/SCA/Scala+Plugin+for+IntelliJ+IDEAor can be installed directoly from within the IDE using Settings -> Plugins dialog. Afterwards one can just do File -> New Project -> Scala -> SBT based. IntelliJ will generate basic build.sbt, download necessary dependencies and open project.

IntelliJ 插件可以在这里找到 http://confluence.jetbrains.com/display/SCA/Scala+Plugin+for+IntelliJ+IDEA或者可以使用设置 -> 插件对话框从 IDE 中直接安装。之后你可以只做 File -> New Project -> Scala -> SBT based。IntelliJ 将生成基本的 build.sbt,下载必要的依赖项并打开项目。

SBT Plugin

SBT插件

Sbt plugin that generate an idea project based on the sbt files can be found here: https://github.com/mpeltonen/sbt-idea

可以在此处找到基于 sbt 文件生成创意项目的 Sbt 插件:https: //github.com/mpeltonen/sbt-idea

SBT 12.0+ & 13.0+

SBT 12.0+ & 13.0+

Simply add addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")to your build.sbt; no additional resolvers are needed.

只需添加addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")到您的build.sbt; 不需要额外的解析器。

Older Versions:

旧版本:

SBT 0.11+

SBT 0.11+

Create and add the following lines to ~/.sbt/plugins/build.sbtOR PROJECT_DIR/project/plugins.sbt

创建以下行并将其添加到~/.sbt/plugins/build.sbtORPROJECT_DIR/project/plugins.sbt

resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

Use gen-ideain sbt to create IDEA project files.

gen-idea在 sbt 中使用来创建 IDEA 项目文件。

By default, classifiers (i.e. sources and javadocs) of sbt and library dependencies are loaded if found and references added to IDEA project files. If you don't want to download/reference them, use command gen-idea no-classifiers no-sbt-classifiers.

默认情况下,如果找到并添加到 IDEA 项目文件的引用,则加载 sbt 和库依赖项的分类器(即源和 javadoc)。如果您不想下载/引用它们,请使用 command gen-idea no-classifiers no-sbt-classifiers



SBT 0.10.1(according to the plugin author, 0.10.0 won'twork!)

SBT 0.10.1(根据插件作者,0.10.0将无法工作!)

Create and add the following lines to ~/.sbt/plugins/build.sbt:

创建以下行并将其添加到 ~/.sbt/plugins/build.sbt:

resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"

libraryDependencies += "com.github.mpeltonen" %% "sbt-idea" % "0.10.0"

Use gen-ideasbt task to create IDEA project files.

使用gen-ideasbt 任务创建 IDEA 项目文件。

By default, classifiers (i.e. sources and javadocs) of sbt and library dependencies are loaded if found and references added to IDEA project files. If you don't want to download/reference them, use command gen-idea no-classifiers no-sbt-classifiers.

默认情况下,如果找到并添加到 IDEA 项目文件的引用,则加载 sbt 和库依赖项的分类器(即源和 javadoc)。如果您不想下载/引用它们,请使用 command gen-idea no-classifiers no-sbt-classifiers



SBT 0.7

SBT 0.7

To use it, simply run this from your sbt shell, it will use the plugin as an external program:

要使用它,只需从您的 sbt shell 运行它,它将将该插件用作外部程序:

 > *sbtIdeaRepo at http://mpeltonen.github.com/maven/
 > *idea is com.github.mpeltonen sbt-idea-processor 0.4.0
 ...
 > update
 ...
 > idea
 ...

You can also add trait in your project definition, as you want:

您还可以根据需要在项目定义中添加特征:

import sbt._
class MyProject(info: ProjectInfo) extends ParentProject(info) with IdeaProject {
  lazy val mySubProject = project("my-subproject", "my-subproject", new DefaultProject(_) with IdeaProject)
   // ...
}

回答by Synesso

For now I do this by hand. It is quite simple.

现在我手工完成。这很简单。

  1. Create the project with SBT
  2. Create a new IDEA Project with the same root path
  3. Create a module with the same root path
  4. Set src/main/scala as a src path on the module
  5. Set src/test/scala as a test path on the module
  6. Add scala-library.jar as a library
  7. Add lib (if it is present) as a jar directory within a module library
  8. Add lib_managed/compile (if it is present) as a jar directory within a module library
  9. Add lib_managed/test (if it is present) as a jar directory within a module library
  1. 使用 SBT 创建项目
  2. 使用相同的根路径创建一个新的IDEA项目
  3. 创建一个具有相同根路径的模块
  4. 将 src/main/scala 设置为模块上的 src 路径
  5. 将 src/test/scala 设置为模块上的测试路径
  6. 添加 scala-library.jar 作为库
  7. 添加 lib(如果存在)作为模块库中的 jar 目录
  8. 添加 lib_managed/compile(如果存在)作为模块库中的 jar 目录
  9. 添加 lib_managed/test(如果存在)作为模块库中的 jar 目录

That's it from memory. It would be better if it were automated, but it's no big deal as it is now.

凭记忆就是这样。如果它是自动化的会更好,但现在没有什么大不了的。

One note of caution: The above approach doesn't work well with new-school sbt, i.e. versions 0.10 and newer, because it doesn't copy dependencies into lib_managed by default. You can add

一个注意事项:上述方法不适用于 new-school sbt,即 0.10 及更高版本,因为默认情况下它不会将依赖项复制到 lib_managed 中。你可以加

retrieveManaged := true

to your build.sbt to make it copy the dependencies into lib_managed.

到您的 build.sbt 以使其将依赖项复制到 lib_managed 中。

回答by Jacek Laskowski

Tempus fugitand IntelliJ IDEA has become so much better these days. It's 2015 after all, isn't it?

这些天来,Tempus fugit和 IntelliJ IDEA 变得更好了。毕竟是 2015 年,不是吗?

Having said that, the latest version of IntelliJ IDEA 14.0.2supports sbt projects out of the box with the Scala plugin. Just install the plugin and you should be able to open up Scala/sbt projects without much troubles.

话虽如此,最新版本的 IntelliJ IDEA 14.0.2通过 Scala 插件支持开箱即用sbt 项目。只需安装该插件,您就应该能够轻松打开 Scala/sbt 项目。

Scala plugin under Plugins in Preferences

Preferences 中 Plugins 下的 Scala 插件

I'm using the Early Access version of the plugin which is 1.2.67.6.EAP as of the time of the writing.

在撰写本文时,我正在使用插件的 Early Access 版本,即 1.2.67.6.EAP。

With the plugin just point at a sbt project and IDEA is going to offer you a wizard to open that kind of project.

使用插件只需指向一个 sbt 项目,IDEA 将为您提供一个向导来打开那种项目。

Import sbt project

导入sbt项目

About sbt-idea in sbt 0.12.4

关于 sbt 0.12.4 中的 sbt-idea

For sbt 0.12.4the system-wide plugin configuration file - ~/.sbt/plugins/build.sbtor PROJECT_DIR/project/plugins.sbt- should have the following lines:

对于 sbt 0.12.4,系统范围的插件配置文件 -~/.sbt/plugins/build.sbtPROJECT_DIR/project/plugins.sbt- 应该有以下几行:

resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin(dependency="com.github.mpeltonen" % "sbt-idea" % "1.5.0-SNAPSHOT")

Run sbt gen-ideato generate IDEA project files.

运行sbt gen-idea生成IDEA项目文件。

Read the sbt-idea plugin websitefor more up-to-date information. You may also find my blog entry Importing sbt-based project to IntelliJ IDEA 13 (with sbt-idea, Scala 2.11 and sbt 0.12)useful.

阅读sbt-idea 插件网站以获取更多最新信息。您可能还会发现我的博客条目将基于 sbt 的项目导入 IntelliJ IDEA 13(使用 sbt-idea、Scala 2.11 和 sbt 0.12)很有用。

回答by opyate

For sbt 0.7

对于 sbt 0.7

See the answer elsewhere on this page.

请参阅本页其他地方的答案

For sbt 0.10

对于 sbt 0.10

Clone and build Ismael's sbt-idea:

克隆并构建 Ismael 的 sbt-idea:

git clone https://github.com/ijuma/sbt-idea.git
cd sbt-idea
git checkout sbt-0.10
./sbt package

Create an sbt plugin lib directory if you don't have one already

如果您还没有,请创建一个 sbt 插件 lib 目录

mkdir -p  ~/.sbt/plugins/lib

Copy the jar built in step one into here

将第一步中构建的jar复制到这里

cp sbt-idea/target/scala-2.8.1.final/*.jar ~/.sbt/plugins/lib

Restart or reload sbt, then you can run gen-idea(or gen-idea with-classifiersif you want sources and javadoc in intelliJ too)

重新启动或重新加载 sbt,然后您就可以运行gen-idea(或者gen-idea with-classifiers如果您也想要 intelliJ 中的源代码和 javadoc)

Source: Tackers' suggestion on the message group.

来源:Tackers 对消息组的建议

In IntelliJ IDEA 13.x itself

在 IntelliJ IDEA 13.x 本身中

You can open an SBT-based project in IDEA nowadays. It will create the necessary project and modules, and keep your dependencies up-to-date whenever you make changes to the build scripts.

现在你可以在 IDEA 中打开一个基于 SBT 的项目。它将创建必要的项目和模块,并在您对构建脚本进行更改时使您的依赖项保持最新。

回答by andyczerwonka

I just went through all this pain. I spend days trying to get an acceptable environment up and have come to the conclusion that ENSIME, SBT and JRebel are going to be my development environment for some time. Yes, it is going back to Emacs, but ENSIME turns it into a bit or an idea with refactoring, debugging support, navigation, etc. It's not nowhere near as good as Eclipse (Java), but unless the scala plugins work better it's the best we have.

我刚刚经历了所有这些痛苦。我花了几天时间试图建立一个可接受的环境,并得出结论 ENSIME、SBT 和 JRebel 将成为我的开发环境一段时间。是的,它又回到了 Emacs,但 ENSIME 将它变成了一个点子或一个带有重构、调试支持、导航等的想法。它远不及 Eclipse(Java),但除非 scala 插件工作得更好,否则它是我们有最好的。

Until the Scala development environments get up to snuff (Eclipse or IntelliJ) I'm not going to bother. They're just way too buggy.

在 Scala 开发环境达到标准之前(Eclipse 或 IntelliJ),我不会打扰。他们只是太笨拙了。

See the discussion on the lift site.

请参阅有关升降机站点的讨论。

http://groups.google.com/group/liftweb/browse_thread/thread/6e38ae7396575052#

http://groups.google.com/group/liftweb/browse_thread/thread/6e38ae7396575052#

Within that thread, there is a link to a HOWTO for IntelliJ, but although it kinda works, there are many issues that render it a little less that useful.

在该线程中,有一个指向 IntelliJ 的 HOWTO 的链接,但尽管它有点工作,但有许多问题使其不太有用。

http://blog.morroni.com/2010/07/14/setup-intellij-9-for-lift-framework-development/comment-page-1/

http://blog.morroni.com/2010/07/14/setup-intellij-9-for-lift-framework-development/comment-page-1/

回答by Raymond Chenon

The answers are old for 2014. In IntelliJ 13.x, the plugin Scala is ver 0.41.2 ( SBT is included).

2014 年的答案是旧的。在IntelliJ 13.x 中,插件 Scala 是 0.41.2 版(包括 SBT)。

My SBT version is 0.13.5(terminal : sbt sbtVersion)

SBT版本是0.13.5(终端: sbt sbtVersion

Go to the project's root folder and enter in the terminal

转到项目的根文件夹并在终端中输入

sbt idea

You will see two new hidden folders .ideaand .idea_modules.

您将看到两个新的隐藏文件夹.idea.idea_modules

Then in IntelliJ, File > Open > select the project. It should open the project without any problem.

然后在 IntelliJ 中,文件 > 打开 > 选择项目。它应该可以毫无问题地打开项目。

回答by Prasad

Before you start creating your SBT project, make sure that the Scala plugin is downloaded and enabled in IntelliJ IDEA.

在开始创建 SBT 项目之前,请确保已在 IntelliJ IDEA 中下载并启用了 Scala 插件。

below link explains everything you need to know.

下面的链接解释了你需要知道的一切。

https://www.jetbrains.com/help/idea/2016.1/getting-started-with-sbt.html

https://www.jetbrains.com/help/idea/2016.1/getting-started-with-sbt.html