设置你喜欢的IDE

本文翻译自Play framework官网:
https://www.playframework.com/documentation/2.4.x/IDE

使用Play非常容易,甚至不需要复杂的IDE。因为Play自动编译并且刷新你的改动到源文件,所以你用简单的文本编辑器就可以轻松工作了。

然而,现代Java或Scala IDE提供了非常酷的功能,比如自动编译、动态(on-the-fly)编译、辅助重构和调试。

Eclipse
Setup sbteclipse
Play需要 sbteclipse 4.0.0 或更新版本.

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

运行eclipse命令之前必须编译你的工程。加上下面的设置就可以在运行eclipse命令时强制编译:
// Compile the project before generating Eclipse files, so that generated .scala or .class files for views and routes are present
EclipseKeys.preTasks := Seq(compile in Compile)


如果你的工程里有Scala源文件,需要安装 Scala IDE

如果你不想安装Scala IDE并且你的工程里只有Java源文件,可以如下设置:

EclipseKeys.projectFlavor := EclipseProjectFlavor.Java           // Java project. Don't expect Scala IDE
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)  // Use .class files instead of generated .scala files for views and routes


配置生成
Play提供了一个命令来简化Eclipse配置。使用"eclipse"命令来转化Play应用为Eclipse工程。

[my-first-app] $ eclipse

如果希望带上可用的jars源(需要多花些时间,而且可能丢掉一些源):

[my-first-app] $ eclipse with-source=true

注意: 如果你带着aggregate使用子工程,需要在build.sbt中设置skipParents。

EclipseKeys.skipParents in ThisBuild := false

或者从Play控制台输入:

[my-first-app] $ eclipse skip-parents=false

然后使用"File/Import/General/Existing project…"导入你的play应用到Workspace.



要想调试,用"activator -jvm-debug 9999 run"启动你的应用,并且在Eclipse中选中工程右键点击并学则" Debug As, Debug Configurations".在"Debug Configurations"对话框中,右键点击" Remote Java Application"并选择" New".修改" Port"为9999并点击" Apply"。现在开始可以点击" Debug"来连接运行中的应用。停止调试会话不会停止服务器。

提示: 可以在应用中使用"~run"来启用文件改变时直接编译。使用这种方法,当你在视图中创建新模板会自动发现scala模板文件,并且文件改变时自动编译。如果使用正常的run命令,则必须每次在浏览器上点击"刷新"。

如果你在应用中有重要改动,比如改变classpath,需要使用eclipse命令重新生成配置文件。

提示:如果是团队工作,不要提交Eclipse配置文件。

生成的配置文件包括框架安装的绝对引用。它们仅适用于你自己的安装。如果是团队工作,每个开发者必须适用他自己的Eclipse配置文件。

IntelliJ

IntelliJ IDEA可以不使用命令提示就快速创建Play应用。IDE之外不需要配置任何东西,sbt编译工具会下载合适的库,解决依赖问题,并构造工程。

在IntelliJ IDEA中创建Play应用之前,要确保最新的Scala Plugin已安装且在IntelliJ IDEA中已启用。即使你不用Scala开发,它也会帮助模板引擎并解决依赖问题。

创建Play应用:
1. 打开"New Project"向导, 在"Scala"选项下选择"Play 2.x",点击"Next".
2. 填写工程信息,点击"Finish".

IntelliJ IDEA使用SBT创建一个空的应用。

也可以导入已有的Play工程。

导入Play工程:
1. 打开工程向导,选择"Import Project".
2. 在打开的窗口里,选择希望导入的工程,点击"OK".
3. 在向导的下一页,选择"Import project from external model",选择"SBT project",点击"Next".
4. 在向导的下一页,选择"Additional import",点击"Finish".

检查工程结构,确保所有必需的依赖已经下载。可以使用code assistance, navigation和on-th-fly code analysis等功能。

至此可以运行创建的应用,并在默认浏览器访问http://localhost:9000看到结果。运行Play应用:
1. 创建新的运行配置 - 从主菜单,选择Run -> Edit来配置;
2. 点击"+"来添加新配置;
3. 从配置列表中选择"SBT Task";
4. 在"tasks"输入框中,简单输入"run";
5. 选择"OK"保存变化;
6. 现在可以从Run菜单中选择"Run"来运行你的应用。

使用默认的Run/Debug配置就可以很容易为Play应用启动调试会话。

更多信息,请查看Play Framework 2.x手册:
https://confluence.jetbrains.com/display/IntelliJIDEA/Play+Framework+2.0

从错误页面导航到源代码

使用play.editor配置选项,可以设置Play的错误页面添加超链接。这样,可以很容易从错误页面导航到IntelliJ, 直接进入源代码(需要先安装 远程调用IntelliJ插件)。

Netbeans

生成配置

Play不支持原生的生成Netbeans工程,但是有一个NetBeans的Scala插件可以帮助Scala语言和SBT:
https://github.com/dcaoyuan/nbscala

还有一个SBT插件来创建Netbeans工程定义:
https://github.com/dcaoyuan/nbsbt

ENSIME

安装ENSIME
请参照安装指导: https://github.com/ensime/ensime-emacs

生成配置

编辑project/plugins.sbt文件,添加下面一行(需要首先检查插件的最新版本 https://github.com/ensime/ensime-sbt):

addSbtPlugin("org.ensime" % "ensime-sbt" % "0.1.5-SNAPSHOT")

启动Play:
$ activator

在Play控制台输入'ensime generate'。插件应当生成一个.ensime文件,位于Play工程的根目录。
$ [MYPROJECT] ensime generate
[info] Gathering project information...
[info] Processing project: ProjectRef(file:/Users/aemon/projects/www/MYPROJECT/,MYPROJECT)...
[info]  Reading setting: name...
[info]  Reading setting: organization...
[info]  Reading setting: version...
[info]  Reading setting: scala-version...
[info]  Reading setting: module-name...
[info]  Evaluating task: project-dependencies...
[info]  Evaluating task: unmanaged-classpath...
[info]  Evaluating task: managed-classpath...
[info] Updating {file:/Users/aemon/projects/www/MYPROJECT/}MYPROJECT...
[info] Done updating.
[info]  Evaluating task: internal-dependency-classpath...
[info]  Evaluating task: unmanaged-classpath...
[info]  Evaluating task: managed-classpath...
[info]  Evaluating task: internal-dependency-classpath...
[info] Compiling 5 Scala sources and 1 Java source to /Users/aemon/projects/www/MYPROJECT/target/scala-2.9.1/classes...
[info]  Evaluating task: exported-products...
[info]  Evaluating task: unmanaged-classpath...
[info]  Evaluating task: managed-classpath...
[info]  Evaluating task: internal-dependency-classpath...
[info]  Evaluating task: exported-products...
[info]  Reading setting: source-directories...
[info]  Reading setting: source-directories...
[info]  Reading setting: class-directory...
[info]  Reading setting: class-directory...
[info]  Reading setting: ensime-config...
[info] Wrote configuration to .ensime


启动ENSIME

在Emacs中, 执行M-x ensime并遵循显示的指导。

至此为止,你的Play项目应该有了类型检查,自动补全等功能。 注意,如果添加新的库依赖,需要重新运行"ensime generate"并重启ENSIME.

更多信息
ENSIME READEME: https://github.com/ensime/ensime-emacs。如果有问题,请在ensime组发帖: https://groups.google.com/forum/?fromgroups=#!forum/ensime

所有Scala插件
Scala是一种新的编程语言,它的很多功能不在核心IDE中,而是在插件中。

1. Eclipse Scala IDE: http://scala-ide.org/
2. NetBeans Scala Plugin: https://github.com/dcaoyuan/nbscala
3. IntelliJ IDEA Scala Plugin: http://confluence.jetbrains.net/display/SCA/Scala+Plugin+for+IntelliJ+IDEA
4. IntelliJ IDEA’s plugin is under active development, and so using the nightly build may give you additional functionality at the cost of some minor hiccups.
5. Nika (11.x) Plugin Repository: https://www.jetbrains.com/idea/plugins/scala-nightly-nika.xml
6. Leda (12.x) Plugin Repository: https://www.jetbrains.com/idea/plugins/scala-nightly-leda.xml
7. IntelliJ IDEA Play plugin (available only for Leda 12.x): http://plugins.intellij.net/plugin/?idea&pluginId=7080
8. ENSIME - Scala IDE Mode for Emacs: https://github.com/aemoncannon/ensime
(see below for ENSIME/Play instructions)

下一章: Play应用结构

猜你喜欢

转载自chenjd.iteye.com/blog/2225796