简体   繁体   English

在IntelliJ(SBT)中配置Scala

[英]Configuration of Scala in IntelliJ (SBT)

I followed the getting started guide on Scala's website and once I switched from IDEA to SBT I started having problems. 我遵循了Scala网站上的入门指南,从IDEA切换到SBT后,我开始遇到问题。 First, it didn't recognize the Scala framework and set the source folder, so I had to do that manually. 首先,它无法识别Scala框架并设置源文件夹,因此我不得不手动执行此操作。 (This is the easy part, just seems weird it doesn't do that by itself...) Second, IntelliJ doesn't seem to recognize the code inside build.sbt. (这是简单的部分,看起来很奇怪,它本身并没有做到这一点...)其次,IntelliJ似乎无法识别build.sbt中的代码。 ('Cannot resolve symbol' everywhere). (“无处无法解析符号”)。 I'm not sure if it actually affects the compilation or not, but I would rather have it working as intended. 我不确定它是否真正影响编译,但我希望它按预期工作。 Third, it doesn't recognize the scalatest library, that I added following this guide - https://docs.scala-lang.org/getting-started-intellij-track/testing-scala-in-intellij-with-scalatest.html . 第三,它无法识别scalatest库,我是根据本指南添加的-https://docs.scala-lang.org/getting-started-intellij-track/testing-scala-in-intellij-with-scalatest 。 html (I first need to add the dependency to the build.sbt file but since it basically doesn't recognize the code there, this might be the problem). (我首先需要将依赖项添加到build.sbt文件中,但是由于它基本上无法识别那里的代码,因此可能是问题所在)。

So this is about it. 就是这样。 I'm not sure what I'm doing wrong and if it's just a stupid mistake on my part, I prefer not losing my head trying to figure it out by myself. 我不确定自己在做什么错,如果这只是我自己的愚蠢错误,我宁愿不遗余力地自己弄清楚。 Also, if there's a more recommended way to work with Scala I'd love to hear it. 另外,如果有推荐使用Scala的方法,我也很想听听。 The only reason I tried doing it in IntelliJ is because I recently worked in it and it seemed pretty nice. 我尝试在IntelliJ中执行此操作的唯一原因是因为我最近在其中进行过操作,而且看起来还不错。

Thanks! 谢谢! (Also sorry if I messed up a bit, this is my first post) (也很抱歉,如果我搞砸了,这是我的第一篇文章)

First of all have you installed the sbt and Scala does it available for your cmd if yes then good otherwise do it first otherwise sbt will not be available for your terminal too. 首先,您已经安装了sbt,如果是,Scala可以在您的cmd上使用它,然后很好,否则请先进行安装,否则sbt也将无法在您的终端上使用。

Ok now once you have done that just go into project settings ctrl+alt+s go into plugin and install sbt build tool plugin from the install plugin browse repository you will find sbt build tool install it. 好了,一旦完成,只需进入项目设置ctrl+alt+s进入插件并从安装插件浏览存储库安装sbt构建工具插件,您将发现sbt构建工具对其进行安装。 It will help you to recognise your build.sbt now setup Scala sdk for your project so go into the project settings ctrl +shift+alt +s and in top left click on + sign and choose jdk then select your Scala sdk from your location (most probably in program files Scala/bin dir). 这将帮助您识别build.sbt,现在为您的项目设置Scala sdk,因此请进入项目设置ctrl +shift+alt +s然后在左上角单击+号并选择jdk,然后从您的位置选择您的Scala sdk(最有可能在程序文件Scala / bin目录中)。

Hope it will help you with your problem. 希望它能帮助您解决问题。

First of all, IntelliJ Idea proposes to install Scala plugin when initially configured after installation. 首先,IntelliJ Idea建议在安装后进行初始配置时安装Scala插件。 So, if you have IntelliJ Idea just for Scala and it still does not work anyway (no settings that are bad to lose and you have the Community version of Idea), then I would recommend reconfiguring it from scratch: close Idea, rename .IdeaIC201x.y in your home directory (in case you have IntelliJ Idea Ultimate this directory probably has slightly different name, and I'm not sure you will not have problems with your license , so it would probably better to skip this step) and restart Idea. 因此,如果您仅拥有适用于Scala的IntelliJ Idea,但仍然无法正常工作(没有任何设置值得丢失,并且您拥有Idea的社区版本),那么我建议您从头开始重新配置它:关闭Idea,重命名.IdeaIC201x.y在您的主目录中(如果您使用的是IntelliJ Idea Ultimate,则该目录的名称可能略有不同,并且我不确定您的许可证不会有问题 ,因此最好跳过此步骤)并重新启动Idea 。 Then on fifth page of the configuration wizard select Scala plugin installation. 然后在配置向导的第五页上,选择Scala插件安装。 Do not install separate SBT plugins. 不要安装单独的SBT插件。

In case reconfiguring IntelliJ Idea is not an option for you, then you can try to fix existing installation: remove already installed plugins related to Scala or SBT and then install just the Scala plugin using "Install JetBrains plugin" button . 如果无法重新配置IntelliJ Idea,则可以尝试修复现有安装:删除与Scala或SBT相关的已安装插件,然后使用“安装JetBrains插件”按钮仅安装Scala插件 It already has built-in and up-to-date SBT support. 它已经具有内置的和最新的SBT支持。

Now your IntelliJ Idea should have very good support for Scala and SBT and it's time to create your Scala project. 现在,您的IntelliJ Idea应该对Scala和SBT具有很好的支持,现在该创建您的Scala项目了。 I usually just import a project checked out from VCS, but let's try to create a project from scratch. 我通常只导入从VCS中签出的项目,但让我们尝试从头开始创建一个项目。 :) Go to New project -> Scala -> sbt . :)转到“ New project -> Scala -> sbt Now Idea will create you a build.sbt file, run sbt and try to import project settings from it. 现在,Idea将为您创建一个build.sbt文件,运行sbt并尝试从中导入项目设置。 Unfortunately, sbt hangs at first initialization sometimes. 不幸的是, sbt有时会在首次初始化时挂起。 For me it helps to just forcefully restart it (but wait enough time before and after), but I don't know the proper way. 对我来说,强制重新启动它会有所帮助(但要在前后等待足够的时间),但是我不知道正确的方法。 After import, you have your project directory structure set up, now you can start coding. 导入后,您已经设置了项目目录结构,现在可以开始编码了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM