How to configure Ivy cache directory per-user or system-wide?

為{幸葍}努か 提交于 2019-11-27 11:14:27
Joachim Hofer

You can simply add an environment variable to your sbt launch shell script:

java -Dsbt.ivy.home=/tmp/.ivy2/ ...

See Library Management in the official documentation.

The sbt.ivy.home property 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.

To change that location, you must set the ivy.home property. So, to augment Joachim's first solution, you would set both system properties:

java -Dsbt.ivy.home=/tmp/.ivy2/ -Divy.home=/tmp/.ivy2/ -jar `dirname $0`/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.

You should use sbt-extras if you don't do already.

Then, it's simply a flag you pass it:

sbt -ivy /path/to/.ivy2

Location of ivy files

I normally put the ivy.xml and ivysettings.xml files alongside by build file as follows:

build.xml
ivy.xml
ivysettings.xml

The ivy tasks resolve and retrieve should find both files.

For example:

<target name="init" description="--> retrieve dependencies with ivy">
    <ivy:retrieve pattern="lib/[conf]/[artifact].[ext]"/>
</target>

Odd, that it's not working for you.

User specific settings

You can emulate the maven settings file in a couple of ways

1) include directive within the project ivysettings.xml

<ivysettings>
    <include file="${user.home}/.ivy2/my-ivysettings.xml"/>
</ivysettings>

2) Set location from the build file

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

3) I've never tried this but I think you can override the default location using an ANT property

ant -Divy.settings.file=$HOME/.ivy2/my-ivysettings.xml

You can retrieve your home directory using Path.userHome.absolutePath, like shown below:

resolvers += Resolver.file("Local", file( Path.userHome.absolutePath + "/.ivy2/local"))(Resolver.ivyStylePatterns)

I suppose that you can also retrieve environment variables using System.getenv and concatenate in the same way, like shown below:

resolvers += Resolver.file("Local", file( System.getenv("IVY_HOME") + "/whatever/it/is"))(Resolver.ivyStylePatterns)
Joachim Hofer

For editing the cache location during the SBT boot itself, see Sbt Launcher Configuration in the official documentation.

Basically, to get it to work system-wide, you'd have to:

  • Put a configuration file named sbt.boot.properties somewhere 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.properties set to point to your configuration file.
  • Set the cache-directory entry (in the [ivy] section) to the location of your ivy cache.

This configuration doesn't seem to carry over to normal SBT usage, though, unfortunately.

sbt -ivy /tmp/.ivy2 compile

Reference: man sbt

Options: -ivy path: path to local Ivy repository (default: ~/.ivy2)

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