38

I am using SBT as my build tool for building a Scala project.

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.

With maven there is the per-user settings.xml that can be used to configure the local repository.

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.

Community
  • 1
  • 1
Ruediger Keller
  • 3,024
  • 2
  • 20
  • 17

7 Answers7

50

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.

Mark Tye
  • 1,661
  • 16
  • 10
  • Thanks - this worked for me on sbt 0.7.7 whereas the other solution is sbt 0.9 and up-specific. – Daniel Martin Dec 14 '11 at 16:25
  • 5
    I know this is an old post, but this info was excellent and if anyone needs - you can add the information above into the file "sbtconfig.txt" located in the folder SBT/Conf at the bottom: -Dsbt.ivy.home=j:/tmp/.ivy2/ -Divy.home=j:/tmp/.ivy2/ – LaloInDublin Jan 26 '14 at 19:02
  • 2
    @LaloInDublin "sbtconfig.txt" (Windows only) is a configuration file that lists JVM options, and is used by sbt.bat. sbtopts is used by sbt shell script. – Epicurist Mar 09 '17 at 15:02
  • And for Intellij setup see this "teliatko/ivy-home-sbt-idea.md" https://gist.github.com/teliatko/127f4ec7e5362f1f5cc68ade9f1221ac – SemanticBeeng Nov 19 '18 at 11:15
  • 1
    Better to use IntelliJ's UI. Setting > Build, Execution > Build Tools > sbt. And add the ```-Dsbt.ivy.home=/tmp/.ivy2/ -Divy.home=/tmp/.ivy2/``` in the VM parameter box. – Epicurist Feb 21 '20 at 09:12
32

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.

turtlemonvh
  • 9,149
  • 6
  • 47
  • 53
Joachim Hofer
  • 344
  • 2
  • 3
8

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
opyate
  • 5,388
  • 1
  • 37
  • 64
  • seems to be simply ignored for me - running `sbt -ivy ~/.ivy2 App/run` - still getting `[error] (PlayCommons / update) java.io.IOException: Permission denied, file: /root/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.8.jar` – AxelTheGerman Oct 07 '20 at 20:40
3

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)
Richard Gomes
  • 5,675
  • 2
  • 44
  • 50
3

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
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • 4
    Thank you for your answer, but it seems to me you are assuming I am using Ant together with Ivy. Maybe it isn't clear from my question, but I am using SBT as my build tool, not Ant. I will edit my question accordingly. – Ruediger Keller Jul 01 '10 at 08:49
2

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.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Joachim Hofer
  • 240
  • 2
  • 8
  • I thought I looked multiple times through the SBT documentation, but I never stumbled upon this. This looks like the proper answer to my question. Therefore I will flag this as the correct answer, after testing this. – Ruediger Keller Nov 09 '10 at 08:04
  • It took me embarassingly long to find this, too... - beware that it doesn't move your Ivy home completely, just the cache directory. It's also very useful in the context of adding your own proxy (like Artifactory or Nexus) into the boot resolver chain. – Joachim Hofer Nov 09 '10 at 08:14
  • Actually, this properties file seems to work only during the boot process of SBT. :( - So, it looks like we're back to the user.home hack... – Joachim Hofer Nov 09 '10 at 09:56
2
sbt -ivy /tmp/.ivy2 compile

Reference: man sbt

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

Mcmil
  • 795
  • 9
  • 25
  • seems to be simply ignored for me - running `sbt -ivy ~/.ivy2 App/run` - still getting `[error] (PlayCommons / update) java.io.IOException: Permission denied, file: /root/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.8.jar` – AxelTheGerman Oct 07 '20 at 20:40