如何在 Play Framework 和 Scala 中使用 IntelliJ

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16135716/
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 05:12:26  来源:igfitidea点击:

How to use IntelliJ with Play Framework and Scala

scalaplayframeworkintellij-idea

提问by noplay

I am trying to use IntelliJ with a play framework 2.11 application.

我正在尝试将 IntelliJ 与播放框架 2.11 应用程序一起使用。

I installed the Play Framework 2 plugin and the Scala plugin for IntelliJ.

我为 IntelliJ 安装了 Play Framework 2 插件和 Scala 插件。

I created a Play application. I have been struggling writing and running Specs 2 tests in IntelliJ. My run config says to run "make" first when running the Specs 2 test, however it doesn't look like my test classes are being generated. Keeps on telling me that it could not find the specification. When I look on the file system, there is no code in target/test-classes, the directory is empty. Further, it seems to take a LONG time to do the build, at least compared to running the Play console.

我创建了一个 Play 应用程序。我一直在努力在 IntelliJ 中编写和运行 Specs 2 测试。我的运行配置说在运行 Specs 2 测试时首先运行“make”,但是它看起来不像我的测试类正在生成。一直告诉我它找不到规范。当我查看文件系统时,目标/测试类中没有代码,目录为空。此外,至少与运行 Play 控制台相比,构建构建似乎需要很长时间。

I wanted to see how people are using Play with IntelliJ. Do you just use IntelliJ as an editor, and run everything through the Play console?

我想看看人们如何通过 IntelliJ 使用 Play。您是否只是将 IntelliJ 用作编辑器,然后通过 Play 控制台运行所有内容?

Is there a way whereby you can run your Application tests in IntelliJ (getting your test classes to run)?

有没有办法可以在 IntelliJ 中运行应用程序测试(让测试类运行)?

I have never had any problem running the Play console and running ~test-only test=xxx.Spec. It has typically been rather fast.

我在运行 Play 控制台和运行 ~test-only test=xxx.Spec 时从来没有遇到过任何问题。它通常相当快。

Here is the exception I am getting in IntelliJ when I try to run my Specs2 tests:

这是我尝试运行 Specs2 测试时在 IntelliJ 中遇到的异常:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.runSingleTest(JavaSpecs2Runner.java:130)
    at org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.main(JavaSpecs2Runner.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.RuntimeException: can not create specification: test.ApplicationSpec
    at scala.sys.package$.error(package.scala:27)
    at org.specs2.specification.SpecificationStructure$.createSpecification(BaseSpecification.scala:96)
    at org.specs2.runner.ClassRunner.createSpecification(ClassRunner.scala:64)
    at org.specs2.runner.ClassRunner.start(ClassRunner.scala:35)
    at org.specs2.runner.ClassRunner.main(ClassRunner.scala:28)
    at org.specs2.runner.NotifierRunner.main(NotifierRunner.scala:24)
    ... 11 more

回答by akkie

Update: In newer versions if IntelliJ IDEA, it is no longer necessary to create the module from play/activator. IntelliJ IDEA has now a really good support for SBT projects. If exists, delete all the idea related directories inside your project. Then in IntelliJ IDEA click File -> Open and choose your build.sbt file. That's all.

更新:在较新版本中,如果 IntelliJ IDEA,不再需要从 play/activator 创建模块。IntelliJ IDEA 现在对 SBT 项目有很好的支持。如果存在,请删除项目中所有与想法相关的目录。然后在 IntelliJ IDEA 中单击 File -> Open 并选择您的 build.sbt 文件。就这样。



IntelliJ IDEA has a good integration for the Play Framework 2. Sometimes it jams, but most of the time it runs. I use it to run(single, all) tests, start or debug the play application and edit my code (o; And this all from within the IDE and without the sbt console.

IntelliJ IDEA 对 Play Framework 2 有很好的集成。有时它会卡住,但大多数时候它会运行。我用它来运行(单个,所有)测试,启动或调试播放应用程序并编辑我的代码(o; 这一切都在 IDE 中进行,没有 sbt 控制台。

Here is a short tutorial with the most important steps. Currently I use IntelliJ IDEA 12.1 with the newest Play Framework 2 and Scala plugins.

这是一个包含最重要步骤的简短教程。目前我使用 IntelliJ IDEA 12.1 和最新的 Play Framework 2 和 Scala 插件。

1. Create a new application

1. 创建一个新的应用程序

play new myapp

2. Create the IDE module

2.创建IDE模块

Start the play console:

启动游戏控制台:

cd newapp
play

Create the module:

创建模块:

idea with-sources=yes
exit

3. Configure the IDE

3.配置IDE

  1. Open the newly created project
  2. Open the module settings(select project and press F4)
  3. Add the Scala library to your project
    1. Select Modules->myapp->Dependencies
    2. Press the plus Icon and select Library(2)
    3. Add the Scala 2.10.0 Project Library
  4. Select the Compiler Library in the Scala Facet
    1. Select Facets->Scala(myapp)
    2. Set the Compiler libraryto Scala 2.10.0
  5. Fix the errors
    1. Select Modules->myapp-build->Dependencies->scala-2.9.2and press the minus icon
    2. Select Libraries->Scala 2.9.2and press the minus icon
  6. Fix the output Path for the myapp-buildmodule
    1. Select Modules->myapp-build->Paths
    2. Append classesto the Output path(X:\projects\myapp\project\target\scala_2.9.2\classes)
    3. Append test-classesto the Test output path(X:\projects\myapp\project\target\scala_2.9.2\test-classes)
  1. 打开新创建的项目
  2. 打开模块设置(选择项目并按F4)
  3. 将 Scala 库添加到您的项目中
    1. 选择Modules->myapp->Dependencies
    2. 按加号图标并选择库(2)
    3. 添加 Scala 2.10.0 项目库
  4. 在 Scala Facet 中选择编译器库
    1. 选择Facets->Scala(myapp)
    2. 编译器库设置为 Scala 2.10.0
  5. 修复错误
    1. 选择Modules->myapp-build->Dependencies->scala-2.9.2并按下减号图标
    2. 选择Libraries->Scala 2.9.2并按减号图标
  6. 修复myapp-build模块 的输出路径
    1. 选择模块->myapp-build->Paths
    2. 附加到输出路径(X:\projects\myapp\project\target\scala_2.9.2\classes)
    3. 测试类附加到测试输出路径(X:\projects\myapp\project\target\scala_2.9.2\test-classes)

4. Run a test

4. 运行测试

Select the ApplicationSpecunder the test directory and click Run 'ApplicationSpec'from the context menu. You should get an error that the compiled template could not be found. This is because the IDE doesn't compile the templates, but this can be done by run the application once. Also follow point 5 and then run the test again.

选择test 目录下的ApplicationSpec,然后从上下文菜单中单击Run 'ApplicationSpec'。你应该得到一个错误,提示找不到编译的模板。这是因为 IDE 不编译模板,但这可以通过运行应用程序一次来完成。同样按照第 5 点,然后再次运行测试。

5. Run the application

5. 运行应用程序

Select a controller and click Run Play 2 Appfrom context menu. This should start the application on address: http://localhost:9000/.

选择一个控制器,然后从上下文菜单中单击Run Play 2 App。这应该在地址上启动应用程序:http://localhost:9000/

6. Update dependencies

6. 更新依赖

If you update your application dependencies then you must tell the IDE about this changes. Also after running the play updatecommand you must close the IDE and remove some files from project directory. If you execute the play ideacommand before removing the files, you get double dependencies in your play project.

如果更新应用程序依赖项,则必须将这些更改告知 IDE。同样在运行play update命令后,您必须关闭 IDE 并从项目目录中删除一些文件。如果您play idea在删除文件之前执行该命令,则会在您的播放项目中获得双重依赖。

Execute the following steps to update your dependencies:

执行以下步骤来更新您的依赖项:

  1. Run the updatetask from your play console
  2. Remove the .idea_modulesand .idea/librariesdirectories
  3. Run the idea with-sources=yescommand in the play console
  4. Run step 3 again
  1. update从您的游戏控制台运行任务
  2. 删除.idea_modules.idea/libraries目录
  3. idea with-sources=yes在播放控制台中运行命令
  4. 再次运行第 3 步

回答by Yeonho

Play console includes a fork of a sbt plugin named sbt-idea. The play's fork got a little lagged behind the original plugin, and has some problems in IntelliJ when you run play idea. You can use the original plugin, which doesn't have any issues. In order to use this plugin in your play project, you need to..

Play 控制台包含一个名为sbt-idea的 sbt 插件的分支。play 的 fork 有点落后于原来的插件,运行时 IntelliJ 有一些问题play idea。您可以使用原始插件,它没有任何问题。为了在你的游戏项目中使用这个插件,你需要......

1.Add the following lines to project/plugins.sbtfile: (the blank line in the middle is required)

1.在project/plugins.sbt文件中加入以下几行:(中间的空行是必须的)

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

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

2.Run gen-ideafrom the play console.

2.gen-idea从播放控制台运行。

回答by Wayan Wiprayoga

I usually used IntellijIDEA (version 12.0.4) only for Play Framework code editor because of:

我通常只将 IntellijIDEA(版本 12.0.4)用于 Play Framework 代码编辑器,因为:

  1. Auto saving feature by default
  2. Rather fast IntelliSense feature
  3. Dracula UI, it is an eye-pleasure for me
  1. 默认自动保存功能
  2. 相当快的 IntelliSense 功能
  3. Dracula UI,对我来说是一种视觉享受

And I usually run and debug the apps with Play SBT console. It's reasonable fast. But, sometimes when executing playcommand on console, I found that the loading-project-infotask take too long time (nearly 10 minutes). I don't know why this sometimes happen, but overall the use of Play SBT console is my choice.

我通常使用 Play SBT 控制台运行和调试应用程序。这是合理的快速。但是,有时play在控制台执行命令时,我发现loading-project-info任务花费的时间太长(近 10 分钟)。我不知道为什么有时会发生这种情况,但总的来说,使用 Play SBT 控制台是我的选择。

I'm doing that because there is usually should-not-be-the-problem-thingsintepreted as errorlike following :

我这样做是因为通常不应该是问题的事情被解释为如下错误

IDEA Error

想法错误

The first time when I start learning Play Framework, I was facing such problem. So, at the end, I choose to use Play SBT console to run and debugging app then.

我第一次开始学习 Play Framework 的时候,就遇到了这样的问题。所以,最后,我选择使用 Play SBT 控制台来运行和调试应用程序。

And sorry I cannot answer for the question number 2. Until now I only tried running and debugging play application. For testing purpose I've never tried before for Play 2.x.

很抱歉,我无法回答第 2 个问题。到目前为止,我只尝试运行和调试播放应用程序。出于测试目的,我以前从未尝试过 Play 2.x。