SBT multi project in Intellij - scala

Im trying to use a single Intellij IDEA window with multi projects (that are not related) imported.
The problem that I'm facing is when I'm executing a sbt task, the task is not loaded in the right project directory.
This is what I did (all in the Intellij Idea window):
Opened a root empty sbt project.
In the sbt toolbar, I clicked the + and added 2 projects job-service and user-service (each was its own build.sbt file)
Pressed Reimport All sbt projects just for fun
Executed compile (from the sbt toolbar) for the user-service
The sbt shell opened, and started compiling the empty root project:
[info] Set current project to root (in build file:/Users/dima/git/root/)
[IJ]sbt:root> compile
what I've expected is
[info] Set current project to user-service (in build file:/Users/dima/git/user-service/)
And of course, the same for the second project job-service
Versions:
Idea version: Version: 2019.2 Build: 192.5728.98
SBT version = 0.13.18
I know the way to use a single build.sbt file with multi projects. But those projects are not related. each project has its own build.sbt file.
I just want to use a single Idea window with multiple projects. and that the sbt toolbar could execute the tasks to the right project
Thanks!
EDIT:
This is the process to recreate what I'm facing:
open project named root2
Import another sbt project called learner
Compile the learner project from the sbt toolbar!
The Problem: sbt shell sets current project to root2
the same for publishLocal task. it sets the current project to root2 and publishes a root2 artifact.
what should have been, is that the sbt would set the current project to learner

I have this problem on IntelliJ some time ago, but in that moment my project was not sbt related. That project contains two different applications in different programming languages, so they could not have a common build file.
But I've found a solution to solve this problem which was using IntelliJ Modules. These Modules will let you create different projects inside your current workspace.
You can access it in File -> Project Structure... -> Modules (In the left panel)
I think you can use this feature to accomplish your expected result. Hope it helps!

Related

sbt Project name must be valid Scala identifier error while creating new project

I wanted to create a new project in scala in intellij. I have chosen Lightbend Project Starter and then Akka Quickstart Scala. On finish I got this error:
sbt Project name must be valid Scala identifier
The name of my project is "akka-demo" but "akkademo" and default "untitled" don't work either. Empty catalog is created. Same with other templates. How can I overcome this?
Env:
Intellij idea ultimate 18.3.4
sbt 1.2.6
As mentioned, akkademo should indeed work.
It sounds like an IntelliJ problem.
If a project directory was created, you can try to remove the .idea directory and reimport the project.
Furthermore, you can download the Akka Quickstart Scala project from Lightbend's website, and start the sbt shell in this directory.
Try to run and compile to make sure your environment is set up correctly.
If everything works with the sbt shell but not in IntelliJ, then open:
IntelliJ -> Preferences -> Build, Execution, Deployment -> Build Tools -> sbt and check the Use sbt shell for imports and for builds.
Then go to the sbt tab in IntelliJ and refresh.

sbt dependency root catalog differs from intellij libraries after import

I'm using Scala plugin version 1.3.3 for Intellij 14.0.3 at my work computer and have started a new SBT project from scratch without any hassle.
But my problems starts right here where build.sbt file have compile errors in Intellij, it can not resolve any line of code. I can how ever auto import by changing the build.sbt file and adding a library dependencies..
So I tested to add scalatest which is downloaded to an .ivy2 directory, but a totaly different one that Intellij are using.
This is how ever how my project structure are looking like, every libs have an error due to wrong path.
And here is where SBT plugin is locating all of it's dependencies and scala libs.
I know how to change where .ivy2 directory will be stored by adding these two parameters to
Settings -> Build,Execution, Deployment -> Build Tools -> SBT -> JVM Options -> VM parameters
-Dsbt.ivy.home=c:/.ivy2
-Dsbt.home=c:/.ivy2
But it only works for .iv2 folders and not the .sbt folders that are also in the wrong place. I believe that's the cause of why I can't resolve the symbols in the build.sbt script.
Does anyone know why this is happening and how I just can have one directory for both Scala plugin and Itellij project files?

sbt eclipse command changed files and packages

I created a new Scala project in eclipse then added a package and Scala object ,
So far so good ...
i want to add external library so i added a project folder with build.properties plugins.sbt files,and another file build.sbt in the root project.
in the terminal i compiled successfully the project with the sbt compile task.
the problem is that after sbt eclipse command the eclipse project changed from Scala project to something else.... all the packages changed to simple folders and the Scala project is ruined
scala IDE :Build id: 3.0.3-20140327-1716-Typesafe
scala version :2.10.4
sbt version:0.13.0
you can see in the image
Where did you get the eclipse command from? I'm guessing you're using sbteclipse.
I created a new Scala project in eclipse then added a package and Scala object , So far so good ...
If I understand correctly, this is exactly the opposite of what the plugin is intended to do. I think you're suppose to create a plain sbt project, and then let the plugin generate the Eclipse project.

Editing Build.scala in Intellij

I want to migrate our build from maven to SBT, so now I work separatedly on Build.scala file. However I don't benefit from any syntax highlighting (that is quite obvious, I don't have SBT in my classpath). What is the correct way to get SBT to my classpath, adding sbt-launch.jar does not seem to help.
IntelliJ 13 has built-in SBT support; if you're running a lower version, then you can have a look at their sbt plugin.
There's also an sbt plugin on github for generating idea project files. I've had success with running the gen-idea task it provides.
Run sbt gen-idea. It will create a "YOUR_PROJECT_NAME - build" project within your main project (for whatever your YOUR_PROJECT_NAME happens to be). Under the project folder of the build project, I was able to write a Build.scala with the following code:
import sbt._
object Build extends Build {
}
The SBT Build trait is recognized just fine. I'm running IntelliJ 13 build #IU-133-696, Scala plugin 0.30.378.
Eventually what I did is finished to write my build.sbt stub, and opened a project with it. Now everything seem to work.

How to add Jar libraries to an IntelliJ Idea SBT Scala project?

I've created an IntelliJ Idea SBT Scala project like Heiko Seeberger's article describes.
I'd like to add a Jar library (joda-time) to use in this project. How to do this? Simply putting them into project's /lib does not help. If I right-click "External libraries" in Idea's project tree, "New >" is greyed.
In the IntelliJ Idea window of your project, got to File >> Project structure >> Libraries. After clicking that Libraries option, two panes will show up. At the top of the left-most pane, click the green "+" button.
The better way to do it is to add your unmanaged dependencies to your build.sbt and refrain from leaving part of your dependency management to your IDE.
Refer to http://www.scala-sbt.org/release/docs/Library-Management.html for details on how to define your unmanagedBase and unmanagedJars tasks.
Just Declare this in build.sbt
unmanagedJars in Compile += file(Path.userHome+"/Your-Jar-Path/Full-Jar-Name.jar")
and required jar will appear in External Library>unmanaged-jars>Full-Jar-Name.jar. This will also change if the jar file(in the provided path) is modified.
In Intellij Idea:
File > Project Structure > Libraries
In Netbeans:
File > Project Properties > Libraries
In Eclipse:
Right click the project > Properties > Java Build Path > Libraries
For a multi-module SBT project (Intellij 2017.3.4, Scala 12.2.4, sbt 1.1.1), the accepted solution only worked until restart or a project refresh. Indeed, "Project Settings-> Modules -> Dependencies", then "+" and "JARs or directories" gives a warning "Module X is imported from Sbt. Any changes made in its configuration may be lost after reimporting".
Possible workaround:
The suggestion by #zero worked for me as follows:
Put the JAR(s) into the project's
lib directory.
In build.sbt, inside lazy var baseSettings = Seq( ... ) add the line
unmanagedJars in Compile += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar").
Still no luck? In the SBT tool window, in a module's sbt settings under unmanagedBase, unmanagedSourceDirectories (and the like) try calling the pop-up commands "Show value" and "Inspect" a few times. Somehow, it might work.
From Eugene Yokota's answer to How can I add unmanaged JARs in sbt-assembly to the final fat JAR? another option (which I didn't try) is to add an individual lib directory to each required module.
Hopefully, these steps will solve the problem or at least help debugging.
Create a lib directory in your project root path, paste your JARs in there, run sbt reload, close the project and open it again. Works for me in IntelliJ 2018.2.4 and SBT 1.0
Go to File -> Project Structure -> Modules
Select module in left pane and go to Dependencies tab
Click + to add JARs or directories
One way we can do is by adding jar explicitly in the Intellij but the problem with this approach is every time we build the project we need to add it again.
First way to do is :
In Intellij Idea:
File > Project Structure > Libraries
Another way is to publish the jar in local
Step 1 : Place the jar in any accessible folder.
Step 2 Create a build.sbt file which contains the detail of the jar.
Step 3 Open cmd to that path and execute command sbt publishLocal.
Step 4 Build your code and you would observe that you are able to access the classes belong to that jar.
build.sbt would look something like this:
organization := "com.abc.core"
name := "abc-test-logging"
version := "1.0-SNAPSHOT"
crossPaths := false
packageBin in Compile := file(s"${name.value}-${version.value}.jar")