Can I avoid compiling sources twice when running play2 and eclipse? - 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.

Related

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.

Problems compiling and running Scala projects in IntelliJ Idea

I have installed the latest IntelliJ Idea vertion with the Scala plugin. I have created a SBT Scala project with the IDE.
Unfortunately, I am not able to compile or run even the simplest "Hello World" example.
object Main {
def main(args: Array[String]): Unit ={
val x: Int = 5
println("Hello Scala!")
println(x)
}
}
If I try to run it, it says that "Error: could not find main class Main".
Rebuilding the project does not help.
BUT, if I run the "sbt" terminal program and execute "run", everything runs fine. Even more, it compiles the necessary class file so that IntelliJ Idea is able to run it after this step.
But, whenever I change something in the code and try to rebuild it from Idea, it will fail as before.
Edit: in Eclipse everything runs ok.
UPDATE
If I try executing "compile" from the sbt shell and then "Run" with the IDE, it will work. But, it will not do it while executing "Build" or "Rebuild Project". Sometimes, but unfortunately not always sp as to be able to reproduce it, it will throw me an exception with "Could not initialize class sbt.internal.io.Milli$"
UPDATE 2nd:
I have simplified even more the task. Now I have something like the following screenshot:
Screenshot IntelliJ IDEA
UPDATE 3rd:
There were two errors in my deployment. One of them was that, as it was pointed before, there was some package definition problems. All the source code should depend on src/main/scala, but that is not enough. So as to use the SBT structure for construction, I had to go to
File -> Settings -> Build, Execution, Deployment -> Build Tools -> sbt
and check "Use auto-import" and "use sbt shell for build and import".
After that, everything runs ok. Finall!
Thanks a lot tro everyone for the useful input!
On your screenshot Runner is in wrong package. It's written package main.scala.
If Runner.scala is in src/main/scala/way/to/my/package package declaration should be package way.to.my.package (if it's just in src/main/scala there should not be line package ...).
Also if still necessary you can try (from what should be tried first to what should be tried last, if things tried before didn't help)
sbt clean
reimport the project to IntelliJ IDEA
File -> Invalidate Caches / Restart ...
delete .idea subfolder of project folder
Did you try the green arrow near "object Main"?
This should start the program.

Facing issue while trying to enable source code in IntelliJ for scala

I am new to Scala programming and I have just started with basic programs in scala in IntelliJ IDE.
When i use the below code
package com.allaboutscala.chapter.one.tutorial_04
object HelloWorld extends App{
println("hello from hello world")
}
I wanted to see the source code of App where it uses the main method, but when i tries to see the source code and when I try to download it, I am getting error like
Sources not found: Sources for 'scala-library.jar' not found. I have attached the screenshot also.It would be great if someone could guide me here.
Since you are using sbt, you need to enable downloading of sources for your project:
open IntelliJ Preferences
search "sbt" or navigate to Preferences | Build, Execution, Deployment | Build Tools | sbt
select "Download: library sources"
refresh the sbt project

"object index is not a member of package views.html" when opening scala play project in scala ide

I've created a play project with play 2.3.7
In the project directory, I ran activator and ran the eclipse command to generate eclipse project files.
When I go to eclipse (I'm using the Scala IDE from typesafe Build id: 4.0.0-vfinal-20150119-1023-Typesafe , there is an error in my Application.scala file:
object index is not a member of package views.html
Is there something amiss with my setup? The app runs fine when I execute run at the activation console prompt.
Thanks!
EDIT: Added code
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
The error is on the 'Ok..' line.
There is a file in views called index.scala.html, and the app runs file when I run it from activator..
Occasionally after adding a view in Play 2.4.x, IntelliJ IDEA sometimes gets confused and absolutely refuses to build. Even rebuild Project fails:
This still happens from time-to-time in IDEA 15. And when it does, the command line provides the quickest, most-reliable fix:
sbt clean; sbt compile
That's it! IDEA will now compile the project as expected.
Update:
In the rare case that sbt compile completed successfully on the command line, but IntelliJ IDEA 15 still gives the same "object x is not a member" error, then this has solved IDEA's confusion:
File Menu:
The other solutions did not work for me. Some would give me different errors, some would clear the Problems tab but leave me with a red squiggle under views.html.index and auto-complete would not work with the scala.html templates.
What finally worked was to open the project's properties, go to Java Build Path > Source, and add both of the following directories:
target/scala-2.11/src_managed/main
target/scala-2.11/twirl/main
If you only do target/scala-2.11/twirl/main then you'll miss out on the class files generated from the conf directory.
In Scala IDE 4.0.0 thinks there's errors in an out-of-the-box Play Framework 2.3.7 program you can find the solution (in brief: adding target/scala-2.11/twirl/main folder to the compilation path), give it a try.
I had the same problem. I added target/scala-2.x/classes and target/scala-2.x/classes_managed to my Java build path and Eclipse stopped complaining.
Adding target/scala-2.11/twirl/main which is having views.html package to source fixed for me.
I had the same issue running Play 2.4.0-RC1 using default SBT layout (disablePlugins(PlayLayoutPlugin)) and solved it by adding to build.sbt:
sourceDirectories in (Compile, TwirlKeys.compileTemplates) :=
(unmanagedSourceDirectories in Compile).value
#brent-foust 's answer worked for me but only initially. Every time I rebuilt the project from within IDEA I would then get "not found: routes" errors from within target\scala-2.11\twirl\main\views\html\main.template.scala until I performed Brent's workaround again.
I eventually discovered the solution to that was changing a line in the .iml file from
<excludeFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" />
to
<sourceFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" isTestSource="false" />
I don't know what the long term implications of doing this are but it has fixed this problem. Some of the other similar problems mentioned might also be fixed by applying the same change to some of the other folders listed in the .iml.
I tried all solutions without any positiv result.
So I went to Preferences > Build, Execution, Deployment > Build Tools > sbt and checked Use sbt shell for imports and builds.
This let the compile button in intelliJ compile with the sbt shell.
I think this is better anyway, since a build server or something similar will compile the same way and not like intelliJ.
For me when importing the project into intellij making sure I "checked" the "auto import" checkbox did the trick.
1) Add the following line to your sbt.build file:
EclipseKeys.preTasks := Seq(compile in Compile)
2) Add the follwing line to your plugins.sbt file under the project folder:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")
3) Run the "eclipse" command from within sbt
as explained in the documentation of the play framework:
Setting up your preferred IDE
Basically we need a way to put the compiled classes on the path for this to work.
I did the following to fix it.
Since the projects compiles to the target directory.
I went to the Project Properties -> Java Build Path and added a few folders that look like this,
target/scala-2.12/routes/main
target/scala-2.12/twirl
target/scala-2.12/twirl/main
Now i dont want you to assume you will have these exact folders in your case too. That depends on your project setup.
But you should add the folders inside the target/scala-2.x folder.

trouble with configuring scala project in intellij

I have been following the getting started with IntelliJ and Scala video from JetBrains and running into two problems.
I can't get it create or start a run configuration
I don't see the scala-test library as a selection under ProjectStructure-Modules-ChooseLibraries
What I've done so far is
Install Scala, add path and environment variables
Install Scala intellij plugin
Create new project set project sdk to java 1.7 and scala home to /usr/local/share/scala-2.10.3
Create an object that extends from App with a simple write line:
The one source file object
object HelloWorld{
def main(args: Array[String]) {
println("hello")
}
}
In the video they right click on the object file and can see a selection of run, but in my case I only see run as Scala Console. I can't seem to get the debugger to work and when I try to create a run configuration as an "Application" it says the src file is "not acceptable"
I'll recommend my simple project skeleton, to get you quickly up and running with Intellij, SBT and even Eclipse setup. Hope it helps!