55

TL;DR: How can I fully reconcile all dependencies in IntelliJ when SBT file changes

I have a SBT project setup in IntelliJ. For the most part if works like a charm, but it is quite a pain for me, to change the version of the dependencies and compilers.

What I hope to do, is to update my SBT file, and click something like clean project in IntelliJ, so it gets up to date.

The best menu item I have found is Rebuild project. It reads new dependencies, but keeps the old ones around, so External Libraries contains multiple versions of the Scala runtime and 3rd party libraries.

To get around this I can close IntelliJ and delete all files in .idea/libraries. When I restart IntelliJ it will re-resolve the dependencies. It works, but manually deleting metadata files indicates that I am doing the wrong thing.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Tobber
  • 7,211
  • 8
  • 33
  • 56

10 Answers10

17

To refresh IntelliJ after changes made in *.sbt files:

  • Open the SBT Project pane (Menu View / Tool WIndows / SBT)
  • Right click on your SBT project
  • Select Refresh external project

enter image description here

Polymerase
  • 6,311
  • 11
  • 47
  • 65
  • 2
    If there are stale compiled classes, you may have to delete the target and project/target directories to get the classes to recompile. – jsears Dec 27 '17 at 15:54
  • 2
    there's also a need to run `sbt clean` and `sbt compile`. – hongsy Sep 22 '20 at 15:46
9

When you save the SBT file, IntelliJ IDEA normally refreshes the project. But if it doesn't, you can do it manually.

There is a Refresh All SBT Projects button in the SBT Panel:

enter image description here enter image description here

On the occasion that IDEA is still confused, not finding packages you just added:

From the File menu:

Invalidate Cache ScreenShot

It's a pretty big hammer, requiring a restart of the app, and multi-minute rebuild of the IntelliSense index. But it's occasionally the only thing that will get IDEA to recognize your new SBT packages.

Brent Faust
  • 9,103
  • 6
  • 53
  • 57
  • Didn't work for me. Only this helped "...close IntelliJ and delete all files in .idea/libraries" – Sasha Oct 26 '15 at 09:51
  • 1
    I think this would be still essential to clean sbt project as "refresh all SBT projects" does not clean the project properly. – Acewin Sep 19 '17 at 17:59
6

try following steps:

  1. close IntelliJ
  2. backup whole project folder
  3. delete .idea folder
  4. delete target folder
  5. delete project/target folder
  6. reopen IntelliJ and import as a sbt project

Note this is only for SBT projects

Max
  • 2,065
  • 24
  • 20
5

Another heavy hammer but seems to work.

  1. Close IntelliJ
  2. Delete .idea folder
  3. Import app from existing sources
Jawa
  • 2,336
  • 6
  • 34
  • 39
cevaris
  • 5,671
  • 2
  • 49
  • 34
4

Has happened to me before, and yes, is a PITA.

What I usually do is to manually remove the dependencies from the project, all of them, then let sbt re-import what is actually needed. Don't do it on the UI though, since even having multiple selection, will ask for confirmation for EVERY dependency! (you will be clicking "Yes, I'm sure" for an hour), instead, go and delete directly from the .iml project file on your .idea/modules directory.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
  • deleting from the .iml file keeps the libraries around. I want to refresh all the libraries too – Martin Klosi Jun 30 '16 at 13:51
  • not sure what you mean by that. The libraries will still be around in any case in your Maven or Ivy folders. There would just not be references from your project. You can delete cached dependencies of course if you want to. – Daniel Langdon Jun 30 '16 at 15:17
  • hm just tested it and it worked, but the exact steps are: 1-quit ide 2-delete content in .idea/modules/.iml 3-launch ide -> throws a bunch of errors 5-refresh dependencies from sbt menu -> old libraries in libraries tab in project structure are indeed gone. I was doing it before without quitting ide. still not sure why would it refresh the library tab if im deleting entries in iml file of only one module? – Martin Klosi Jul 01 '16 at 02:53
2

I was using different versions of sbt and scala across different proejcts and IntelJi got confused somehow. I am only able to resolve the issue by removing the sbt and ivy2 cache folders:

rm -rf ~/.ivy2
rm -rf ~/.sbt
Yuchen
  • 30,852
  • 26
  • 164
  • 234
1

Agree, that's a PITA. Nothing helped me (I tried "Invalidate/Restart", removing files from ".idea/libraries" and restarting).

The only way to overcome the problem for me was to manually remove project-related folders from the IntelliJIdea cache. I removed these folders from the following places (I use OSX and IntelliJ-Idea 14):

  • /Users/{UserName}/Library/Caches/IntelliJIdea14/compiler/{ProjectName}
  • /Users/{UserName}/Library/Caches/IntelliJIdea14/compile-server/{ProjectName}

This helped me, hope it will save time for someone.

alekseevi15
  • 1,732
  • 2
  • 16
  • 20
1

If you are using the activator through a terminal/shell/command prompt use the following command:

activator clean

Or just "clean" on the sbt shell through intellij

It should help reset things and next time you run or compile, it should go through it from scratch.

Saleh
  • 347
  • 2
  • 11
0

IDEA 2016.2.4

None of these solutions worked for me.

**** BEFORE YOU TRY THE ANY OF ABOVE. MAKE A BACKUP OF THE SBT / IDEA PROJECT IN QUESTION *****

If I updated the 'build.sbt' then the external library dependencies on the project view did not update. Moreover, the Project Structure still showed the old dependencies. No matter what I tried IDEA would not refresh.

The only way I could get a partial restore was to copy the '.idea' folder from another machine. SBT plugin implementation is ****ed! I removed the '.idea/libraries' folder beforehand and then IDEA did not restore this folder. I tried many times importing the project again.

Very bad :(

ADDENDUM: I managed to get my project working. I think SBT with IDEA is no longer at fault entirely. It could be just possible that my Apache Ivy 2 cache might have been corrupted. So I executed the following commands:

$ rm -rf ~/.ivy2/cache/org.scalatest/scalatest_2.11/*
$ rm -rf ~/.ivy2/cache/org.scalactic/scalactic_2.11/*

Restarted IDEA with invalidate cache under the file drop down menu item. This seemed to trigger some internal action in IDEA and SBT. I also tried manually adding add JARs throught the IDE, but it got massively confused. Moral of the story is to work on two different laptops machines daily and keep them updated.

Also double check SBT from the command line works. I was using 0.13.12

$ sbt about
$ sbt clean
$ sbt test

Also recheck with the SBT dependencies plugin too.

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
peter_pilgrim
  • 1,160
  • 11
  • 18
0

What worked for me was to:

1) Close IntelliJ

2) delete .idea/libraries internals

3) Start IntelliJ

4) right-clicked pom.xml Maven > Reimport

After that there were no old versions and duplicate libraries in the External Libraries.

Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106