Setup Play Framework with Scala in IntelliJ

UPDATE: This is only for Play 1.x with Scala. Getting Play 2 projects (Java or Scala) working in IDEA is now as easy running “play idea” and then opening the project.
Update 2: I’ve posted a new video showing how to create and run Play2 apps in IntelliJ.

Yesterday at the Programming Summer Camp a group of us were working together to learn the Play Framework. Once we were able to get the basics working we wanted to get everything working in IntelliJ IDEA. Due to a lack of good documentation on the subject things did not go smoothly until we figured out the right “magical incantations”. We did eventually get it working so I wanted to document the steps we took.

  1. Add Scala support to Play:
    play install scala
  2. Create a new Play project with Scala support:
    play new foo --with scala
  3. Have Play create an IntelliJ Module Descriptor:
    play idealize foo
  4. Run the app:
    play run foo
  5. Access the app in a browser to generate some source files we will use later:
    http://localhost:9000
  6. Shutdown the Play server.
  7. Currently the Play Scala Module (version 0.9.1) only supports Scala 2.8.1 so download and extract that version.
  8. Create a new project (from scratch) in IntelliJ. The location should be the same as the directory where the Play project was created. Un-check the “Create module” option.
  9. In the Project Structure window, with “Modules” selected, click the “+” button to add a new module to the project.
  10. Select the “Import existing module” option and then point it to the generated .iml file.
  11. With the module now selected, select the content root block for the module (mine is /home/jamesw/projects/foo). Then select the “tmp/generated” directory in the tree on the right and press the “Sources” button. The HTML template pages are converted to .scala source files by Play and we need these source files to be included in the project along with the regular source files in the app directory.
  12. Select “Global Libraries” in the Platform Settings on the left and add a new Java Library named “scala-compiler-2.8.1″. Then press the “Attach Classes…” button and navigate to the “lib” directory in your Scala 2.8.1 directory. Then select the “scala-compiler.jar” and “scala-library.jar” files.
  13. Add another Global Library named “scala-library-2.8.1″ containing the “scala-dbc.jar”, “scala-library.jar”, and “scala-swing.jar” files.
  14. Press the “Apply” button to save the Global Libraries configuration.
  15. Select “Modules” and then the “Dependencies” tab. Press the “Add…” button and select “Library” to add a new library to the module. Select “scala-library-2.8.1″ and then press the “Add Selected” button to add it to the Dependencies.
  16. Select the “Scala” Facet in the module and set the Compiler library to the “scala-compiler-2.8.1″ option.
  17. Save the Project Structure by pressing the “Ok” button. Make sure the project now builds without any errors.
  18. To run the Play server from IntelliJ a new Run Configuration must be configured. To create a new Run Configuration select “Run” from the main IntelliJ menu and then select “Edit Configurations”.
  19. Press the “+” button and select “Application” from the list of Run Configuration types.
  20. Set the name to “Play Server”.
  21. Set the Main class to “play.server.Server”.
  22. Set the VM Parameters to:
    -Dapplication.path="."
  23. De-select the “Make” option in the “Before Launch” section.
  24. Press the “Ok” button to save the configuration.
  25. Run the Play Server by selecting “Run” from the “Run” IntelliJ menu and verify that the application still works by opening the application in a browser.

It is so much easier to figure this stuff out with a group of people. That is just one of the many reasons why the Programming Summer Camp is a great event!

Let me know if you have any questions or problems.

This entry was posted in Play Framework, Scala. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Ivan Meredith

    I find the scala ide (eclipse) better for scala now. For play you just have to get the 2.8.2 version instead of the default 2.9.x version. A lot of the steps are the similar to this post too :). That being said I end up using sublime text/textmate/emacs a lot now too, since play takes care of the compiling.

  • Mecid

    I want to use playframework with java on IDEA, but i have a problem with modules.
    Anything about?

  • http://www.touilleur-express.fr Nicolas Martignole

    For step 12 and 13, you can use scala-library and scala-compiler from play-scala module. You don’t have to download Scala. It’s bundled with the play-scala module and SBT.

    Nicolas

  • http://www.jamesward.com James Ward

    Cool! That is a better way to go. Thanks Nicolas!

  • Jérémy

    We also have to install the scala plugin in order to have a Scala facet available in the settings.
    Thanks !

  • Jérémy

    How can I run in test mode ? (play test)

    • http://www.jamesward.com James Ward

      I think that would just be a Run Configuration parameter. But I’ll have to look into it more.

      • Luc

        Hi James,

        did you find out how to run play test from intellij?

        Regards

        • http://www.jamesward.com James Ward

          Ok, got this one figured out… You have to do two thing:
          1) Add -Dplay.id=test to the VM options in the run configuration
          2) Add play-1.2.3/modules/testrunner/lib/play-testrunner.jar to the module dependencies

          Let me know if that works.

          • Helena Hjertén

            This worked perfectly for me, with Java (don´t know Scala yet).

  • Rob Minson

    Thanks for the very comprehensive guide, seems “idealize” doesn’t quite “play run foo” works fine, but the intellij make project command gives me a bizarre error trying to build the project for the first time:

    error: object TemplateMagic is not a member of package play.templates
    import play.templates.TemplateMagic._

    in a generated .scala file for Application.html.index. This is particularly strange as the exe which generated that file (/usr/local/Cellar/play/1.2.2/libexec/play) was installed (via brew) in a single transaction alongside the jar with the missing class which is /usr/local/Cellar/play/1.2.2/libexec/framework/play-1.2.2.jar (contains the play.templates package, but no TemplateMagic class inside that).

    I could understand the discrepancy if play.templates.TemplateMagic was an external dependency and I’d installed an incompatible version, but they’re packaged alongside eachother – where the hell is that class???

    I’m using play 1.2.2

    • http://www.jamesward.com James Ward

      That is a good question. Seems odd. I wonder if it makes a difference that you installed Play via brew. Maybe the Play Scala module didn’t get installed properly.

  • Serg

    Followed this instructions. I try to start application from IDE with green button.
    Application ‘hello’ is now started ! But when I’m going to browser I’m getting an error.

    Action not found
    Action application.index could not be found. Error raised is Controller controllers.application not found

    Looks like scala compilation not happening. If I run it with play without IDE everything is OK. Is someone else experienced same issue?

  • opensas

    Hi serg, I have exactly the same trouble

    moreover, after trying to run it from intellij, I have to issue a play clean from the command to be able to run it with play run

    when running from intellij the templates are not compiled… could anybody solve this?

    java version “1.6.0_25″
    intellij version: 10.5.2
    scala plugin (0.4.1338)
    Scala code runner version 2.8.1.final — Copyright 2002-2010, LAMP/EPFL

    saludos
    sas

    ps: excellent guide!

    • serg

      I was able to solve this. Open Project Settings, go to dependencies. Make sure you have correct paths to play libs ‘{your play installation}/play/framework/lib’ and to scala module ‘{your play installation}/play/modules/scala-0.9.1/lib’. In my case path to scala module generated by idealize command was incorrect and scala compilation didn’t execute. Once you fix it you should see this line at start after ~ Module scala is available
      02:16:06,974 INFO ~ Scala support is active
      This is my question on stackoverflow

  • http://ivata.com Colin MacLeod

    For me, I now find that the HTML sources are no longer shown when I run Play from within IntelliJ.

    When there is an error, the source code links point to the generated code, not the original .scala.html files. This only happens when I run the app from within IntelliJ – from the command line, the .scala.html sources are linked in the browser (the same, i.e generated, sources are shown in the stack trace).

    I had the feeling this was working when I first set it up, but I have since tried to re-create the project from scratch and it still seems to produce the same results.

    Any ideas?

    PS: This is a great guide – it has saved me a great deal of time. Thank you so much!

    • http://www.jamesward.com James Ward

      Hi Colin,

      Thanks for giving this a try! Unfortunately I’m not sure what the problem might be. Perhaps you can ask in the IntelliJ forums. Sorry.

      -James

  • Pingback: Play! + Scala et la serialization Json « taraxe()

  • William Chan

    Very helpful instructions! I followed most of it.

    It seems strange to me, though, that the scala-library and scala-compiler libraries do not use Scala bundled with the Play Scala project (and called scala-0.9.1 instead of scala-2.8.1). I would like to try to use the same version of Scala as the Play Scala framework.

    This is motivated, for me, by the fact that IntelliJ doesn’t seem to recognize a lot of Scala syntax when I include scala-library-2.8.1 as a project dependency, but IJ seems to recognize a lot more Scala syntax when I leave that library off.

    Play Scala 0.9.1 includes the jars for scala-compiler-2.8.1 in play-1.2.3/modules/scala-0.9.1, but I couldn’t find the analogs for scala-library-2.8.1 in the Play Scala bundle. In other words, I couldn’t find play-1.2.3/modules/scala-0.9.1/lib/scala-dbc.jar, scala-library.jar, or scala-swing.jar. Couldn’t find the corresponding sources either.

    Does this strike anyone else as odd? Shall we request that the Play Scala team include all of this stuff in the framework?

    • http://www.jamesward.com James Ward

      Yes, you are right that we should just point to the jars in the modules/scala-0.9.1 dir. I have both:
      modules/scala-0.9.1/lib/scala-compiler.jar
      modules/scala-0.9.1/lib/scala-library.jar

      But don’t have scala-dbc.jar or scala-swing.jar which probably aren’t needed.

  • Pingback: Tutorial: Play Framework 2 with Scala, Anorm, JSON, CoffeeScript, jQuery & Heroku()

  • Alessandro Bianco

    Nice tutorial, but I had some problems to run the application from IntelliJ, which was strange ’cause (after some struggle) it was working when launched from the console.

    When running the server from IntelliJ, it the application throws a “Application.index not found” error, notice if the console says: 
    INFO  ~ Scala support is active
    when the server starts up.

    If it doesn’t, go to the project settings and under Modules > Dependencies add your “play/modules/scala/lib” folder.

    On the next run everything should be fine and you can enjoy your application.

  • Cwiese

    Apparently Play 2.0 is very different.  Using IntelliJ with Application based server and “play.server.Server” does not exisit.  “play.core.server.NettyServer” does not have a main method.

    The new Play Framework support simply runs Play command like (except from the project folder not the module folder.

    Is play still valid – should I go back to Spring?

    PS) Flex is dead, Javascript is a mess…..I am lost for a new home.

  • Cwiese

    Yes…thanks – read the manual right :-) 

  • Pingback: Play 2.0 with Scala and Scaml, Part1: Setup of test infrastructure, model and persistence with Anorm « Jan's Blog()

  • http://www.facebook.com/pligor George Pligor

    These instructions are outdated! From Play Framework version 2.02 things are easier. All you need to do is just a “play idea” and you are good to go

    • http://www.jamesward.com James Ward

      You are right! I’ve added a note to the top of this blog to point that out.

    • Shwetanka

      But still idea is not able to resolve any extra dependencies added to Build.scala. I’m adding them manually. So it’s good to know.

  • julan

    HI, What you mean with “play idea”?? I can’t find that. Can you help me, Thanks a lot

    • http://www.jamesward.com James Ward

      I’m not sure what you mean.

  • Pingback: Play Framework 2 with Java, Scala ,JSON, CoffeeScript, jQuery | PremAseem.com()



  • View James Ward's profile on LinkedIn