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

纵然是瞬间 提交于 2019-12-05 01:48:41

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!