Getting IntelliJ IDEA understand SBT dependencies - scala

I have SBT and Scala installed and things work well.
Now I have an IntellIJ IDEA project, which works otherwise OK except that dependencies which are downloaded into /Users/MyName/.ivy2/cache/ are not understood.
I tried setting the cache path as a source folder:
but still things don't work out:
As you can see, org.eclipse is in red color meaning it can't find it. It must be inside that .jar file I showed on the left.
I just want to update/download dependencies using SBT, and then get the compilation to work in IntelliJ IDEA with those dependencies.

Use the sbt-idea plugin for SBT. Then you can run "gen-idea" from the sbt command line and it will generate the metadata files used by IDEA. These files contain the locations of jars as SBT uses them so that IDEA will know where the dependencies are, just like SBT.

Related

Inside IDE(IntelliJ), could not find sbt dependies from External Libraries

I am starting a Scala project and I'm using SBT and Intellij 2020.2.3 as my IDE.
I have the following build.sbt file from the project, but I can't seem to get the dependencies in the Idea IDE "External Libraries" section to show up in "libraryDependencies" of SBT after running "sbt update".
The Idea version is that:
The Idea External Libraies are that:
The Sbt libraryDependencies are that:
In my experience , Intellij is very slow to pickup on dependencies and display them.
Bare in mind this is not a reflection on sbt , and asides for the annoyance, you should still be able to sbt compile from the console.
That is the first thing I suggest you test. If you can compile , that means the dependencies were downloaded, are available on the classpath, and its just a matter of getting Intellij to recognize that
You can try one of 2 things, hover over the dependencies in your build.sbt file and you might see a refresh project option , or you can go to module section in project settings and reimport

Intellij sbt won't resolve dependencies' dependencies

I have a Intellij Scala project, that depends on another lib, which in turn depends on some other libs (gson, log4j...), but somehow Intellij won't add those transitive dependencies to my project, so when I run my main method, jvm always complains no class found. I also tried sbt update and sbt update-classifier, they won't resolve down the dependency path as well. How can I have sbt resolve all levels of dependencies?
If all works in SBT command lines but not in Idea then remove the .idea folder and reload the SBT project in Idea.
I should warn that you will loose the project configs (such as run configs) by doing that.
I finally figure out what was wrong, it was the sbt-s3-resolver plugin I was using couldn't resolve maven style dependencies, I used an another plugin which supports maven libs and it worked. But I still couldn't figure out why it worked before.

sbteclipse not adding generated source folders to java build path?

I ran sbt eclipse on a Scala Project and when I imported it into Scala IDE(4.0.0 RC2), it gave me a type not found error as the types referred to were actually auto-generated code which were at target/scala-2_10/src_managed/main/compiled_avro/org/... I was able to do a sbt compile at the console though.
I got it to compile by adding the above folder to the Java Build Path.
My question is that since sbt eclipse can already detect Java Projects which the current project depends on and since sbt compile works at the console, should sbt eclipse be able to figure out dependencies to source folders of generated code as well? or maybe such a feature exists and I just don't know about it?
This may not be the correct way of doing things but to fix the issue i did the following.
sbt avro:compile
sbt compile
sbt eclipse
In eclipse i right clicked on target/scala-*/src_managed/main/compiled_avro > build path > use as source folder
The sbteclipse way:
Edit your project or global build.sbt file. My global ~/.sbt/0.13/build.sbt contains:
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Managed
I'm using an older version of _sbteclipse, version 2.5.0 (various non-relevant reasons), which seems to require both the import and a single blank link between each line of real content (this drives me a bit crazy, yes). I don't believe the import is required for newer versions of sbteclipse.
sbt clean avro:compile compile
sbt eclipse

Intellij unable to resolve references to a specific jar file

In a given project that is driven by sbt there is some kind of corruption in the project libraries specifically for a MavenLocal repository used for kafka-spark-9.7.2.jar in which:
references to the classes provided by that jar are marked as "symbol not found" by the editor parser
however the editor (strangely) does offer to import the classes
but after accepting the import, the symbols are still marked in red as unresolved.
The following attempts to "clean things up" have already been performed:
Build | Make Project
Build | Rebuild Project
In addition I have verified that the project does build from
sbt package
on the command line
UPDATE After re-running sbt gen-idea the librraries are still not found by the Parser. yet the libraries exist -even IJ knows about them as shown in the following screenshot. Why is it that IJ can find the library
C:\Users\S80035683\.ivy2\cache\org.apache.kafka\kafka\jars\kafka-0.7.2-spark.jar!\kafka\api\FetchRequest.class
However IJ is unable to resolve any classes from that library in the Parser?
You have to build the project for Intellij, try to do this in your project root:
sbt idea with-sources=yes
This should build the project structure from scratch and add the right dependencies, usually I refresh the project after adding a dependency or a jar.
Edit:
To use the command you need this plugin, otherwise you can use gen-idea but I used it only a few times and I'm not sure how it will work out.
Edit2:
There was some confusion, first, for the IDEA SBT console you don't need to prepend the sbt command since you already are inside sbt:
If you have the sbt plugin for idea you can use gen-idea with-source=yes (without prepending sbt)
From the terminal, either you go to your project root and type sbt to enter the sbt console and use gen-idea or idea with-sources=yes (without prepending sbt)
or directly sbt gen-idea or if you have the plugin sbt idea with-sources=yes (prepending sbt)
To reach the sbt console inside idea you need to install the sbt plugin on preferences -> plugin and search for sbt and then View -> Tool Windows -> SBT Console:
To start the console click on the play button, to kill the console on the skull.
I had the same problem. I fixed it by directly writing the CLASSES and SOURCES of the problematic library. This can be found in .idea/libraries/SBT__<problematic library>_jar.xml

Unable to make IntelliJ Idea 12 work with Scala compiler

I have installed Scala, sbt, eclipse and IntelliJ Idea 12. And also jdk, jre, etc. I'm able to run scala in Eclipse (Scala eclipse IDE) but I can't do it in Idea, even though I downloaded and installed scala plugin though Idea. Here is what I'm having at File -> Setting
and at a new project creation page
How do I solve these issues?
whereis scala
scala: /usr/bin/scala /usr/bin/X11/scala /usr/share/scala
which scala
/usr/bin/scala
I know I'm repeating this at any possible occasion—but your life will be much easier if you have sbt generate your IDEA project instead of trying to set it up manually. That will take care of configuring the modules correctly, so you are instantly ready to compile and run.
Here is a blog entry that might help. The section "How can I integrate libraries installed by SBT to IDEA?" tells you how to generate the project files.
Basically you need to create—starting from the root directory of your project—the file project/plugins.sbt with the following content:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.4.0")
(you can also do that in the file ~/.sbt/plugins/build.sbt instead, that way you have the plugin available for any of your projects)
Then you run sbt gen-idea, and afterwards you can open the project directly from IDEA through File -> Open Project (and pointing to the project's root directory).
You could also generate your IDEA project with Gradle, which handles Scala+IDEA combination very well. Here's a minimal build.gradle script to do this:
apply plugin: 'scala'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.10.1'
}
Just create a directory for your project, put build.gradle inside it, create src/main/scala subdirectory, then install Gradle and run gradle idea inside your project's directory. That should generate nicely configured IDEA project. With this method you don't even need to install Scala.
What exactly your problem is? I don't see anything on your screens which prevents you from using Scala in IDEA. Just select "Set Scala Home" radiobutton in "New project" dialog and then select your Scala installation path (I guess it will be /usr/share/scala). IDEA then will automatically create library and compiler libraries and add Scala facet to your project.