Scala Play 2.3 InteliJ 14 *community edition - is it possible to run project without cmd line activator command - scala

From http://blog.jetbrains.com/scala/2012/12/28/a-new-way-to-compile/ I understand that Intellij is using sbt to build - so thats 1x build
At the moment I run a separate command line task to run my project -
./activator -jvm-debug 5000 -Dhttp.port=9000
So each code change gets compiled twice (once by InteliJ; once by activator)
Is there a way to use the InteliJ build artifacts when I run, so the code is only compiled once?
Is using the Terminal window in InteliJ any different from a normal terminal window?
The versions I'm at are:
Java 7
Play framework 2.3.4
SBT 0.13.1
InteliJ 14 (community edition)
Thanks
Brent

Yes it's definitelly possible.
Install Play 2.x plugin (with dependencies) and add the run configuration dedicated for Play 2 (note in Intellij 14 Play2 support is combined within the Scala plugin)
Menu > Run > Edit configurations... > Add new configuration (plus icon in the corner) > Play 2 App
After saving you will be able to run your app (also in debug mode) within IntelliJ - i.e. via
Menu > Run > Debug 'Your Project'
btw, you can remove Make (before launch) from there, SBT should handle builds properly without Idea's support
See sample shot

Related

how to debug scala sbt project in vs code

I am trying to debug sbt project in vs code. I have downloaded vs code extension name scala metals. How to explicitly add build.sbt folder path in scala metal.
How to explicitly add build.sbt folder path in scala metal
Open the root of your Scala project via VSCode, and Metals should detect your build.sbt and ask you if you want to load it. Otherwise you can do it explicitly from the Command Palette (Ctrl+Shift+P) and write "Metals Import Build"
how to debug scala sbt project in vs code
Not sure how to do it via metals. But the way I do it, is to open sbt myself from the terminal, via sbt -jvm-debug 5005. Then I attach VSCode remote debugger. You'll have to install "Debugger for Java" VSCode plugin too.
Here is the official documents: https://scalameta.org/metals/docs/editors/vscode.html,
See the "Running and debugging your code" sections.
But I found it's hard for me to debug the web project based on play framework.
So I use inntelij instead.
Start the project in sbt with: sbt --jvm-debug 5006
You can run it in the terminal:
After entered sbt, type "run" to start the proejct.
Add a new debug configuration, set port to: 5006
And then click "run->debug...", you can add the breakpoint.

Cannot purge old classes when using IntelliJ and Payframework project using sbt

Setup details
1. IntelliJ Idea Community Edition 2018.2.5 with sbt plugin
2. PlayFramework
3. scalaVersion 2.12.8
4. Java 1.8 (open JDK)
5. sbt: bundled sbt with play-scala-hello-world-tutorial
6. Host OS: Limux Mint 18.3, KDE Plasma 5
Issue:
I tried using the hello-world project template from Playframework - using IntelliJ to import it as an sbt project and accepting the default settings. After making some edits I deleted the project in the filesystem directly and replaced it with a fresh copy. However, when I run the project using the sbt_command window with the command 'run', the project launches Play bound to port 9000 and the running project still shows the 'old' modified version of the app.
Steps to reproduce the problem:
Load 2.7.x version of play-scala-hello-world-tutorial
Edit HomeController.scala;
from
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
to
def index() = Action { implicit request: Request[AnyContent] =>
Ok("Just Play Scala")
}
sbt_run
Ctrl+D to quit running app (in IDE's sbt command window)
Close IntellijIDEA
Delete project root folder in Idea Workspace
Place with fresh copy of play-scala-hello-world-tutorial scala-seed project template
Import project
Run using sbt 'run' command (in IDE's sbt command window)
view http://localhost:9000/ => results in "Just Play Scala"
Attempted resolutions
- Closed project from IntelliJ and deleted the project root folder from the IntelliJ-Idea workspace on the filesystem
- Performed #1 and deleted .ivy2 and .mvn directories
- Tried to reproduce the problem in a VM running Fedora28 using a text editor to replicate the changes. Everything works as expected. All changes are reloaded dynamically in play and changes are visible in the ruuning app.
Diagnosis
Either IntelliJ does not work as expected/ advertised for recompiling sbt projects or I do not know how to use the IDE to properly purge previously compiled code.

How to execute SBT plugin's tasks from within IntelliJ IDEA CE?

I use IntelliJ IDEA 13.1 Community Edition with the Scala plugin 0.32.593.
What's the recommended way to execute a SBT plugin's tasks, say dependencyUpdates from sbt-updates?
In version 15 of IntelliJ IDEA, using Scala Plugin version 2.0, you can run SBT tasks by adding a run configuration.
First add a new run configuration by clicking on Run -> Edit Configurations. Then click the + button to add a new configuration and choose SBT Task form the list
Now provide the list of tasks separated by space in Tasks input box:
NOTE IntelliJ IDEA and the Scala and sbt plugins have improved since and you may want to read How to run sbt-assembly tasks from within IntelliJ IDEA? instead.
I worked it around with the Embedded Terminal plugin that shipped with the version of IDEA.
Alt+F12 (or Tools > Open Terminal) to open a terminal window and then sbt dependencyUpdates inside.
It does require a local installation of SBT.
Unless there has been a recent change (in 13.1), SBT plugin doesn't support running sbt tasks.
See comments here http://blog.jetbrains.com/scala/2013/11/18/built-in-sbt-support-in-intellij-idea-13/
You will have to run these two commands to run SBT in the terminal:
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java $SBT_OPTS -jar `<SBT inatall>`/bin/sbt-launch.jar "$#"

How to use IntelliJ with Play Framework and Scala

I am trying to use IntelliJ with a play framework 2.11 application.
I installed the Play Framework 2 plugin and the Scala plugin for IntelliJ.
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.
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?
Is there a way whereby you can run your Application tests in IntelliJ (getting your test classes to run)?
I have never had any problem running the Play console and running ~test-only test=xxx.Spec. It has typically been rather fast.
Here is the exception I am getting in IntelliJ when I try to run my Specs2 tests:
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
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 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.
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.
1. Create a new application
play new myapp
2. Create the IDE module
Start the play console:
cd newapp
play
Create the module:
idea with-sources=yes
exit
3. Configure the IDE
Open the newly created project
Open the module settings(select project and press F4)
Add the Scala library to your project
Select Modules->myapp->Dependencies
Press the plus Icon and select Library(2)
Add the Scala 2.10.0 Project Library
Select the Compiler Library in the Scala Facet
Select Facets->Scala(myapp)
Set the Compiler library to Scala 2.10.0
Fix the errors
Select Modules->myapp-build->Dependencies->scala-2.9.2 and press the minus icon
Select Libraries->Scala 2.9.2 and press the minus icon
Fix the output Path for the myapp-build module
Select Modules->myapp-build->Paths
Append classes to the Output path(X:\projects\myapp\project\target\scala_2.9.2\classes)
Append test-classes to the Test output path(X:\projects\myapp\project\target\scala_2.9.2\test-classes)
4. Run a test
Select the ApplicationSpec under 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.
5. Run the application
Select a controller and click Run Play 2 App from context menu. This should start the application on address: http://localhost:9000/.
6. Update dependencies
If you update your application dependencies then you must tell the IDE about this changes. Also after running the play update command you must close the IDE and remove some files from project directory. If you execute the play idea command before removing the files, you get double dependencies in your play project.
Execute the following steps to update your dependencies:
Run the update task from your play console
Remove the .idea_modules and .idea/libraries directories
Run the idea with-sources=yes command in the play console
Run step 3 again
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..
1.Add the following lines to project/plugins.sbt file: (the blank line in the middle is required)
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.1")
2.Run gen-idea from the play console.
I usually used IntellijIDEA (version 12.0.4) only for Play Framework code editor because of:
Auto saving feature by default
Rather fast IntelliSense feature
Dracula UI, it is an eye-pleasure for me
And I usually run and debug the apps with Play SBT console. It's reasonable fast. But, sometimes when executing play command on console, I found that the loading-project-info task 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.
I'm doing that because there is usually should-not-be-the-problem-things intepreted as error like following :
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.
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.

Can I avoid compiling sources twice when running play2 and eclipse?

Currently I am running eclipse and play (with ~run) at the same time. When I change a file it will be compiled by play and by eclipse.
Is it possible to avoid one of those two compilation steps?
As far as I know, the eclipse plugin also uses sbt to build the project so maybe there is a way to execute the play "run" command inside eclipse?
(I am asking because my laptop is not very fast and compilation takes some time, and I would like to have the "Fast turnaround" as advertised on the play webpage ;)
You can turn off Build Automatically from the Project menu without losing any of the IDE functionality. Binaries will be built only by Sbt (on the command line).
A detailed guide for setting-up Play 2 with Scala IDE can be found on the Scala IDE website: http://scala-ide.org/docs/tutorials/play20scalaide20/index.html
I have not yet tried to run the play run sbt task in eclipse.
BUT you can run the server directly from eclipse.
Add "target/scala-2.9.1/classes" to your class path, use filters to include only your assets. (Project Properties, Java Build Path)
Choose "Run Configurations..." from the Run-Button-Menu.
Create a new "Java Application" configuration with your favourite name.
Main Tab: Use "DebugStart" as your main class
Arguments Tab: Configure any "-Dconfig.file=..." "-Dlogger.file" options you might need in VM arguments
Classpath Tab: Add the conf directory to the classpath (Advanced/Add Folders)
Create DebugStart.scala with:
import play.core.server.NettyServer
import java.io.File
import play.core.SBTLink
import play.core.TestApplication
import play.api.test.FakeApplication
import play.api.test.TestServer
object DebugStart {
def main(args: Array[String]) {
val app = FakeApplication()
val server = TestServer(9000, app)
server.start()
}
}
You can now start the app with run or debug. If you use debug, you can perform some code changes without any restart.
My version of DebugStart.scala actually contains some platform dependent hackish code to kill any running process, so that I can just hit F11 or CTRL+F11 to restart the application.
To ensure that your assets/sources are up to date run:
> sbt
...
[your project] $ ~ ;play-copy-assets;sources
IntelliJ Idea 12 (Leda) is coming soon. I'm using 11 for a while and there's no such problems but new version will offer much better Play 2.0 integration.