如何设置 IntelliJ Idea Scala 项目以识别本地 Ivy2 缓存?

Posted

技术标签:

【中文标题】如何设置 IntelliJ Idea Scala 项目以识别本地 Ivy2 缓存?【英文标题】:How to set up an IntelliJ Idea Scala project to recognize local Ivy2 cache? 【发布时间】:2012-03-15 12:49:17 【问题描述】:

您可能知道,新版本的 SBT(如 0.11)旨在维护~/.ivy2/cache 中的公共库缓存,而不是在项目目录中复制完整的依赖项集。但是 IntelliJ Idea(我使用 SBT 插件生成一个 Idea 项目)默认似乎忽略了公共缓存,只看到放在项目目录下的库(因此,我必须配置 SBT 以采用这种传统方式)。如何解决这个问题?

Idea 插件库中有一些 Ivy 插件,但我不知道其中哪些(如果有)可以解决问题(注意我不希望它管理依赖项和下载库(如它由 SBT 完成)但只是为了在缓存中查找类)。

【问题讨论】:

【参考方案1】:

sbt-idea 引用sbt 引用它们的位置中的库,默认情况下位于~/.ivy2/cache 下。

这是它在我的系统上的外观,带有一个新项目,SBT 0.11.2 和 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>

但是,SBT 有一个配置设置,retrieve-managed 用于制作所有库的本地构建副本,而不是直接从缓存中引用它们。如果您启用了此功能,sbt-idea 还将设置 IntelliJ 类路径以引用这些。

这里是帮助:

> help retrieve-managed

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

让我们试试吧:

> 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>

【讨论】:

major +1,同样适用于 Eclipse 还是只有 IntelliJ? no-sbt-classifiers 是做什么的? 我确实一直在使用检索管理来让一切正常工作。上次我尝试避免它会导致 Idea 看到库时出现问题。不过那是 Idea 10,也许它在 11 中修复了...... 它避免了下载 SBT 和插件的源代码/javadoc。这是 sbt-idea 0.11.0 中的错误,它可能会在 1.0.0 中修复。

以上是关于如何设置 IntelliJ Idea Scala 项目以识别本地 Ivy2 缓存?的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA scala的源码设置

Scala的Intellij Idea设置,需要澄清

IntelliJ Idea 中用于 Scala 的模块 SDK

如何在IntelliJ IDEA中运行Java/Scala/Spark程序

scala配置intellij IDEA15.0.3环境及hello world!

intellij idea新建时为啥找不到scala class