How to set up an IntelliJ Idea Scala project to recognize local Ivy2 cache?
Asked Answered
G

1

10

As you probablu know, new versions of SBT (as 0.11) are designed to maintain a common libraries cache in ~/.ivy2/cache instead of copying the full set of dependencies inside a project directory. But IntelliJ Idea (I use the SBT plugin to generate an Idea project) default seems to be ignoring the common cache and only see the libraries put under the project directory (so, I have to configure SBT to act this legacy way). How to fix this?

There are some Ivy plugins in the Idea plugin repository, but I don't know which (if any) of them can solve the problem (note that I don't want it to manage the dependencies and download the libraries (as it is done by SBT) but just to look for classes in the cache).

Griffey answered 25/2, 2012 at 5:8 Comment(0)
S
7

sbt-idea refers to the libraries in the locations that sbt refers to them, which by default is under ~/.ivy2/cache.

Here's how it looks on my system, with a fresh project, SBT 0.11.2, and sbt-idea 0.11.0:

 ~/code/scratch/20120225 sbt
[info] Loading global plugins from /Users/jason/.sbt/plugins
[info] Set current project to default-d73535 (in build file:/Users/jason/code/scratch/20120225/)

> set libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.3"
[info] Reapplying settings...
[info] Set current project to default-d73535 (in build file:/Users/jason/code/scratch/20120225/)

> session save
[info] Reapplying settings...
[info] Set current project to default-d73535 (in build file:/Users/jason/code/scratch/20120225/)

> gen-idea no-sbt-classifiers
[info] Trying to create an Idea module default-d73535
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.scalaz#scalaz-core_2.9.1;6.0.3 ...
[info] Excluding folder target
[info] Created /Users/jason/code/scratch/20120225/.idea/IdeaProject.iml
[info] Created /Users/jason/code/scratch/20120225/.idea
[info] Excluding folder /Users/jason/code/scratch/20120225/target
[info] Created /Users/jason/code/scratch/20120225/.idea_modules/default-d73535.iml
[info] Created /Users/jason/code/scratch/20120225/.idea_modules/project.iml

  ~/code/scratch/20120225 grep jar /Users/jason/code/scratch/20120225/.idea/libraries/*.xml
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-library.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$USER_HOME$/.ivy2/cache/org.scala-lang/scala-library/docs/scala-library-2.9.1-javadoc.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$USER_HOME$/.ivy2/cache/org.scala-lang/scala-library/srcs/scala-library-2.9.1-sources.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$USER_HOME$/.ivy2/cache/org.scalaz/scalaz-core_2.9.1/jars/scalaz-core_2.9.1-6.0.3.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$USER_HOME$/.ivy2/cache/org.scalaz/scalaz-core_2.9.1/docs/scalaz-core_2.9.1-6.0.3-javadoc.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$USER_HOME$/.ivy2/cache/org.scalaz/scalaz-core_2.9.1/srcs/scalaz-core_2.9.1-6.0.3-sources.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/scala_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-library.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/scala_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-compiler.jar!/"></root>

However, SBT has a configuration setting, retrieve-managed to use make a build-local copy of all libraries, rather than directly referring to them from the cache. If you have this enabled, sbt-idea will also setup the IntelliJ classpath to refer to these.

Here's the help:

> help retrieve-managed

If true, enables retrieving dependencies to the current build.  Otherwise, dependencies are used directly from the cache.

Let's try it out:

> set retrieveManaged in ThisBuild := true  
[info] Reapplying settings...
[info] Set current project to default-d73535 (in build file:/Users/jason/code/scratch/20120225/)
> update
[info] Updating {file:/Users/jason/code/scratch/20120225/}default-d73535...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.scalaz#scalaz-core_2.9.1;6.0.3 ...
[info] Done updating.
[success] Total time: 1 s, completed Feb 25, 2012 9:07:41 AM
> show compile:dependency-classpath
[info] Updating {file:/Users/jason/code/scratch/20120225/}default-d73535...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.scalaz#scalaz-core_2.9.1;6.0.3 ...
[info] Done updating.
[info] ArrayBuffer(Attributed(/Users/jason/.sbt/boot/scala-2.9.1/lib/scala-library.jar), Attributed(/Users/jason/code/scratch/20120225/lib_managed/jars/org.scalaz/scalaz-core_2.9.1/scalaz-core_2.9.1-6.0.3.jar))
[success] Total time: 0 s, completed Feb 25, 2012 9:07:54 AM


> gen-idea no-sbt-classifiers
[info] Trying to create an Idea module default-d73535
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.scalaz#scalaz-core_2.9.1;6.0.3 ...
[info] Excluding folder target
[info] Created /Users/jason/code/scratch/20120225/.idea/IdeaProject.iml
[info] Created /Users/jason/code/scratch/20120225/.idea
[info] Excluding folder /Users/jason/code/scratch/20120225/target
[info] Created /Users/jason/code/scratch/20120225/.idea_modules/default-d73535.iml
[info] Created /Users/jason/code/scratch/20120225/.idea_modules/project.iml

 ~/code/scratch/20120225 grep jar /Users/jason/code/scratch/20120225/.idea/libraries/*.xml
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-library.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$PROJECT_DIR$/lib_managed/docs/org.scala-lang/scala-library/scala-library-2.9.1-javadoc.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scala_lang_scala_library_2_9_1.xml:      <root url="jar://$PROJECT_DIR$/lib_managed/srcs/org.scala-lang/scala-library/scala-library-2.9.1-sources.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$PROJECT_DIR$/lib_managed/jars/org.scalaz/scalaz-core_2.9.1/scalaz-core_2.9.1-6.0.3.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$PROJECT_DIR$/lib_managed/docs/org.scalaz/scalaz-core_2.9.1/scalaz-core_2.9.1-6.0.3-javadoc.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/org_scalaz_scalaz_core_2_9_1_6_0_3.xml:      <root url="jar://$PROJECT_DIR$/lib_managed/srcs/org.scalaz/scalaz-core_2.9.1/scalaz-core_2.9.1-6.0.3-sources.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/scala_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-library.jar!/"></root>
/Users/jason/code/scratch/20120225/.idea/libraries/scala_2_9_1.xml:      <root url="jar://$USER_HOME$/.sbt/boot/scala-2.9.1/lib/scala-compiler.jar!/"></root>
Serrated answered 25/2, 2012 at 8:13 Comment(4)
major +1, same applies for Eclipse or is this IntelliJ only?Wessling
What does no-sbt-classifiers do?Humanism
I have been indeed using retrieve-managed for everything to work. The last time I've tried avoiding it lead to problems with Idea seeing the libraries. That was Idea 10 though, maybe it's fixed in 11...Griffey
It avoids downloading the source/javadoc for SBT and plugins. This was buggy in sbt-idea 0.11.0, it might be fixed in 1.0.0.Serrated

© 2022 - 2024 — McMap. All rights reserved.