scala 如何为每个用户或系统范围配置 Ivy 缓存目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3142856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Ivy cache directory per-user or system-wide?
提问by Ruediger Keller
I am using SBTas my build tool for building a Scala project.
我使用SBT作为构建 Scala 项目的构建工具。
My problem is, I can't configure SBT to download dependencies to my user home directory. Therefore I am looking for a per-user or even better a system-wide setting to tell SBT to put the Ivy cache directory somewhere else.
我的问题是,我无法将 SBT 配置为将依赖项下载到我的用户主目录。因此,我正在寻找每个用户甚至更好的系统范围设置来告诉 SBT 将 Ivy 缓存目录放在其他地方。
With maven there is the per-user settings.xml that can be used to configure the local repository.
使用 maven,可以使用每个用户的 settings.xml 来配置本地存储库。
I have read question How to override the location of Ivy's Cache?and it's answers, but it seems it only describes how to configure the setting on a per project basis.
我读过问题如何覆盖常春藤缓存的位置?这是答案,但它似乎只描述了如何在每个项目的基础上配置设置。
If there is no alternative I would go for a per-project setting, but I didn't get the answer from the mentioned question to work. Some more details would be most welcome, for example where to put the ivysettings.xml. I put it into the project's root directory and it didn't work.
如果别无选择,我会针对每个项目进行设置,但我没有从上述问题中得到答案。一些更多的细节将是最受欢迎的,例如放置 ivysettings.xml 的位置。我把它放在项目的根目录中,它不起作用。
采纳答案by Joachim Hofer
You can simply add an environment variable to your sbt launch shell script:
您可以简单地将环境变量添加到您的 sbt 启动 shell 脚本中:
java -Dsbt.ivy.home=/tmp/.ivy2/ ...
See Library Managementin the official documentation.
请参阅官方文档中的库管理。
回答by Mark Tye
The sbt.ivy.homeproperty is only half of the solution. It controls where the sbt launcher downloads sbt itself (and related dependencies like the scala compiler and library, etc.) As noted by Joachim Hofer, it has no effect on where the dependencies declared by your project get downloaded.
该sbt.ivy.home属性只是解决方案的一半。它控制 sbt 启动器在哪里下载 sbt 本身(以及相关的依赖项,如 Scala 编译器和库等)。正如 Joachim Hofer 所指出的,它对下载项目声明的依赖项的位置没有影响。
To change thatlocation, you must set the ivy.homeproperty. So, to augment Joachim's first solution, you would set both system properties:
要更改该位置,您必须设置该ivy.home属性。因此,为了增强 Joachim 的第一个解决方案,您需要设置两个系统属性:
java -Dsbt.ivy.home=/tmp/.ivy2/ -Divy.home=/tmp/.ivy2/ -jar `dirname sbt -ivy /path/to/.ivy2
`/sbt-launch.jar "$@"
With these properties, the launcher will download both your project's and sbt's dependencies to the /tmp/.ivy2/directory. Of course, you can put them in separate directories as well.
使用这些属性,启动器会将您的项目和 sbt 的依赖项下载到该/tmp/.ivy2/目录中。当然,您也可以将它们放在单独的目录中。
回答by opyate
You should use sbt-extrasif you don't do already.
如果您还没有使用sbt-extras,则应该使用它。
Then, it's simply a flag you pass it:
然后,它只是您传递给它的一个标志:
resolvers += Resolver.file("Local", file( Path.userHome.absolutePath + "/.ivy2/local"))(Resolver.ivyStylePatterns)
回答by Richard Gomes
You can retrieve your home directory using Path.userHome.absolutePath, like shown below:
您可以使用 检索您的主目录Path.userHome.absolutePath,如下所示:
resolvers += Resolver.file("Local", file( System.getenv("IVY_HOME") + "/whatever/it/is"))(Resolver.ivyStylePatterns)
I suppose that you can also retrieve environment variables using System.getenvand concatenate in the same way, like shown below:
我想您还可以System.getenv以相同的方式使用和连接来检索环境变量,如下所示:
build.xml
ivy.xml
ivysettings.xml
回答by Mark O'Connor
Location of ivy files
常春藤文件的位置
I normally put the ivy.xmland ivysettings.xmlfiles alongside by build file as follows:
我通常将ivy.xml和ivysettings.xml文件放在构建文件旁边,如下所示:
<target name="init" description="--> retrieve dependencies with ivy">
<ivy:retrieve pattern="lib/[conf]/[artifact].[ext]"/>
</target>
The ivy tasks resolveand retrieveshould find both files.
常春藤任务解析和检索应该找到这两个文件。
For example:
例如:
<ivysettings>
<include file="${user.home}/.ivy2/my-ivysettings.xml"/>
</ivysettings>
Odd, that it's not working for you.
奇怪的是,它不适合你。
User specific settings
用户特定设置
You can emulate the maven settings file in a couple of ways
您可以通过几种方式模拟 Maven 设置文件
1) include directive within the project ivysettings.xml
1) 在项目ivysettings.xml 中包含指令
<target name="init" description="--> retrieve dependencies with ivy">
<ivy:settings file="${user.home}/.ivy2/my-ivysettings.xml" />
<ivy:retrieve pattern="lib/[conf]/[artifact].[ext]"/>
</target>
2) Set location from the build file
2)从构建文件设置位置
ant -Divy.settings.file=$HOME/.ivy2/my-ivysettings.xml
3) I've never tried this but I think you can override the default location using an ANT property
3)我从未尝试过,但我认为您可以使用 ANT 属性覆盖默认位置
sbt -ivy /tmp/.ivy2 compile
回答by Joachim Hofer
For editing the cache location during the SBT boot itself, see Sbt Launcher Configurationin the official documentation.
要在 SBT 启动期间编辑缓存位置,请参阅官方文档中的Sbt Launcher 配置。
Basically, to get it to work system-wide, you'd have to:
基本上,要使其在系统范围内工作,您必须:
- Put a configuration file named
sbt.boot.propertiessomewhere where it's accessible system-wide (the default one is listed at the link above). - Call the launcher with the additional system property
sbt.boot.propertiesset to point to your configuration file. - Set the
cache-directoryentry (in the[ivy]section) to the location of your ivy cache.
- 将一个名为的配置文件
sbt.boot.properties放在系统范围内可访问的某个位置(默认文件列在上面的链接中)。 - 使用
sbt.boot.properties设置为指向您的配置文件的附加系统属性调用启动器。 - 将
cache-directory条目(在该[ivy]部分中)设置为常春藤缓存的位置。
This configuration doesn't seem to carry over to normal SBT usage, though, unfortunately.
然而,不幸的是,这种配置似乎并没有延续到正常的 SBT 使用中。
回答by Milso
Reference: man sbt
参考:man sbt
Options: -ivy path: path to local Ivy repository (default: ~/.ivy2)
选项:-ivy path:本地常春藤存储库的路径(默认:~/.ivy2)

