LoginSignup
13
13

More than 5 years have passed since last update.

EclipseでPlay Framework 2.6 (Java) の環境構築

Last updated at Posted at 2017-12-02

Play Framework 2.6 (Java) の新規プロジェクトを生成して、Eclipseにインポートするまでの手順まとめです。v2.5.xまで使っていたactivatorコマンドはEOLを迎えたので、v2.6.x以降はsbt1を直接使います。

事前条件

  • Java 1.8
  • sbt v0.13.13以降
  • Eclipse インストールされていること

sbtはMacの場合Homebrewで、Windowsの場合ここからでインストールしてください。既にsbtが入っている場合、aboutオプションでバージョン確認が可能です。

$ sbt about
=> [info] This is sbt 1.0.4

プロジェクト作成

sbt new [テンプレート名] で新規プロジェクトを作成します。プロジェクト名やScalaのバージョンなどを聞かれますが、何も入れずにEnterを押せば[]内のデフォルト値が採用されます。

$ sbt new playframework/play-java-seed.g8
[info] Set current project to play (in build file:〜〜〜)

This template generates a Play Java project

name [play-java-seed]:
organization [com.example]:
scala_version [2.12.3]:
play_version [2.6.7]:
sbt_version [1.0.2]:

sbt runでアプリが起動し、http://localhost:9000 から稼働確認ができます(初回実行の場合、ライブラリを落とすので時間がかかる)。

Eclipse用の設定追加

project/plugins.sbtにプラグイン設定を追加。

plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.2")

次に、build.sbtにEclipse用の設定を追加。

build.sbt
EclipseKeys.preTasks := Seq(compile in Compile, compile in Test)
EclipseKeys.projectFlavor := EclipseProjectFlavor.Java
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)

Eclipseにインポート

Eclipse用の.classpathと.projectファイルを生成。

$ sbt eclipse

これでEclipseにインポート (File > Import > Existing Projects into Workspace) できるようになります。ただ、こうしてインポートした状態だと、view (*.scala.html) から生成されたクラスをEclipseが参照しないのでControllerでコンパイルエラーが出ます。これを解消するため、追加でビルドパスを設定します。

  1. Eclipse上でプロジェクトを右クリック > Build Path > Configure Build Path...
  2. LibraryタブからAdd Class Folderを選択
  3. 開いたダイアログでtarget/scala-2.12/classesにチェックを入れてOK

スクリーンショット 2017-12-02 13.47.20.png


  1. Scala向けのビルドツール。activatorは内部的にsbtを利用していた 

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13