SBT Console in Intellij - scala

I am beginner in Scala. I cant find how to add SBT Console.
I have sbt shell, but in Scala course i see that they have both: SBT Shell and SBT Console, and using the second one. I tried to write "console" in sbt shell, and after "run", but it says:
error: not found: value run.
So I just want to add somehow this SBT Console, to run my program not only from button "run" in Intellij.
And what about SBT? Is it really needed? Or can i dont use it for Scala?

You're probably confused by "Shell" and "Console" usually being synonyms, but they actually mean different things with sbt.
sbt shell - is, uhm, a shell that sbt gives you when you run it in interactive mode - i.e. when you just run sbt from your terminal. This gives you access to other exciting things, such as running the app (with run command), inspecting the build (inspect ...), running tests (test, obviously) and so on.
sbt console - is an interactive REPL (read-evaluate-perform-loop) that gives you scala as an interpreter - i.e. you can type in (or paste) some scala code, and it will be immediately compiled and evaluated - outputting the results. Sbt console is also available from sbt shell - by running console.
So, in order to run your program, you have two options (besides the IntelliJ "run" dialog):
From your terminal do sbt run
From your terminal do sbt, then wait for sbt to initialize, then in the sbt shell do just run.
Refer to sbt docs for more details on different ways to run sbt (spoiler: there's also "batch" mode, where you can run sbt run from your terminal - and it is exactly the same as sbt and then run).
Sbt Reference manual might also be helpful - specifically it covers the sbt console command.
And what about SBT? Is it really needed? Or can i dont use it for Scala?
It is not mandatory - you can run scala with maven, it's just a bit more involved to configure. However, if you allow a bit of personal opinion, I'd recommend using sbt with scala projects (and even with Java projects). For one simple reason - the build definition in sbt is essentially just scala code - you can use any language features, third part libraries and even custom code in your build - which sometimes comes very helpful. Compared to maven, I think it's a big step forward, as in maven you define the build using a XML-based "language" defined by the mvn tool and it's plugins - which is much less customizable.
However, gradle also has this advantage, so pick the tool that best suits your needs.

Related

How to get IntelliJ IDEA to `activator publishLocal` as a task?

I'm trying to get IntelliJ IDEA to run activator publishLocal as a task, so that it publishes updated artifacts to the local repository for use by other projects.
If there is a better way to share code between projects without publishing, that would work as well.
You can create an SBT Task in the "Run" section of IntelliJ IDEA, but you can't create an "Activator" task, nor does it seem like you can just run any command-line script.
Activator has been decomissioned, and any support removed from the IntelliJ Scala plugin as of version 2017.3
However, activator was based on sbt, thus the publishLocal task works just as well when run as an sbt task.
You can run a command line script with a Bash run configuration, for instance.

How to log commands issued from Intellij with Scala plugin to SBT

I worked some time with SBT and a text editor. Then, moved to Intellij, installed Scala plugin, and created a new project as in the picture here by choosing it to be based on SBT. Also, in the settings, I chose the SBT launcher to be custom, and made it point to the already-installed sbt.
I believe Intellij is probably calling SBT commands like sbt run and stuff in the background. My question: How can I see (a log of) these commands as they're called by Intellij to sbt and their responses? And as a plus: where can I edit them? I opened the "Edit Configurations" but couldn't find anything like sbt compile.
I believe Intellij shows the responses (e.g., [info] downloading...) below in the "Background Tasks", but
1. It doesn't log these responses and
2. It doesn't show the commands issued by Intellij to sbt.
Big picture/what I'd really want to do: have the IntelliSense features from Intellij and Scala plugin, but build and run from the terminal and not have any effects from Intellij on that.
The IntelliJ Scala plugin doesn't (yet) call sbt except for project import/refresh or if you have a run configuration that uses sbt. Compilation is done by the built-in compile server. If you prefer to use IntelliJ as an editor and run tasks from the sbt console, that usually works fine.
I don't understand the question well
do u want to see the SBT output? if you are using run/debug you will see both the command & the output in the run window (alt+4)
also intelliJ has a window called event log
I hope that help
Intellij reads the projects configuration from sbt and creates an equivalent configurations for its project format (either .idea folder or .ipr)
this is a similar question to yours but about maven, you may find helpful:
https://stackoverflow.com/a/30167052/2675679

How to debug/run a single gatling simulation in IntelliJ IDEA without sbt command?

How could I debug or run the BasicSimulation.scala?
Now I use sbt command to run:
testOnly simulations:BasicSimulation
Is there any other easy way to debug? Thanks.
You can add a simple class and run it by right click->run . Something like here: https://github.com/puppetlabs/gatling-puppet-load-test/blob/master/simulation-runner/src/main/scala/com/puppetlabs/gatling/runner/PuppetGatlingRunner.scala
If you want to run gatling tests inside the intellij, you can go to the edit configurations in the run toolbar on the top left and add a new sbt task.
On the task field enter testOnly simulations:BasicSimulation and that's it.
Normally you should be able to debug it by clicking the debug button, but it doesn't seems to be possible for the gatling tests at least in my configuration.
According to actual documentation this is done like:
gatling:testOnly *BasicSimulation*
Here the Documentation
As with any SBT testing framework, you’ll be able to run Gatling
simulations using SBT standard test, testOnly, testQuick, etc… tasks.
However, since the SBT Plugin introduces many customizations that we
don’t want interfering with unit tests, those commands are integrated
into custom configurations, meaning you’ll need to prefix them with
gatling or gatling-it, eg. gatling:test or gatling-it:test.
(I tested this in sbt console)
Everything is nicely explained in the official documentation page of the gatling project:
http://gatling.io/docs/2.2/extensions/maven_archetype/
Just use their archetype to generate a project in maven and then import in any decent IDE and voilà

How to run tests on every code change in IntelliJ IDEA from Scala sbt project?

I have a new Scala/sbt project set up in IntelliJ IDEA with ScalaTest and jUnit interface successfully installed as dependencies. I have one test that passes when I hit run or debug.
My problem is that it doesn't rerun if I change something. I have to hit run or debug again and then it runs and gives the expected response.
How do I set up IntelliJ IDEA (with or without sbt) to run all tests every time the code changes? Is it possible to run the tests related to the files that were changed only?
As answered in comment by Boris the Spider you can run
> ~test
from sbt shell. Hopefully sbt and IntelliJ can integrate better via sbt server, but I don't think it's currently possible.

Scala+IDEA: Pros and cons of sbt and fsc

I'm currently using IDEA's build mechanism with fsc for developing with Scala. It's still a bit slow and having to (re) start the compilation server is a pain. Many people here are suggesting SBT as a build tool together with IDEA.
What do you consider the pros and cons of each approach?
I tried both and in the end I prefer straight sbt for compiling.
Cons? I really miss being able to click through compile errors and fix the code directly, but... compiling in sbt is just much faster.
The nightly builds of the Idea Scala plugin can vary in quality/performance, but it's been getting better and better lately. The Scala plugin can now flag a number of errors that before I would have had to run compile to catch. (For example, I'm running nightly build 0.4.693 and the new method inspections have already been dead helpful.)
My advice for life with sbt on the command line: start sbt up and leave it running interactively as long as possible to take advantage of everything being loaded and JIT-ed.
sbt left running will fall over eventually but by giving it more memory in your sbt wrapper you can make that happen only rarely.
Here's the sbt launch wrapper that works for me.
java -Xms512M -Xmx1500M -XX:MaxPermSize=512m -jar `dirname $0`/sbt-launch.jar "$#"
My biggest issue with sbt 0.7 is that it frequently goes back and recompiles great swathes of files that seem only tangential to the code I was actually changing. (Even so, still faster than Idea and fsc!)
Good news: sbt 0.9 has some great incremental compile improvements. Unfortunately, the migration path from 0.7 to 0.9 is still in its early days. Mark Harrah's presentation at NEScala is online at http://www.nescala.org/2011/ if you're interested.
Useful plugins
http://github.com/mpeltonen/sbt-idea - easily create and keep your Idea project in sync with your sbt project
http://github.com/orfjackal/idea-sbt-plugin - lets you create run profiles for sbt from within Idea (I found this slower than running sbt at the command line last October - but I see orfjackal is still developing so I should give it another shot)