6

I forked a Scala library from GitHub, and I want to import it to another project.

How can I tell sbt where to find this package?

For example, I'm writing a program in ~/code/scala/myProgram, and I want to import a library from ~/code/scala/otherlib.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Emil
  • 447
  • 4
  • 18

2 Answers2

11

If it is supported by the project you have cloned (that is, if it supports SBT and is configured to publish to a repository), you can publish it locally with the sbt command sbt publish-local. For example:

cd ~/code/scala/otherlib
sbt publish-local

This will build and publish this library in your local Ivy repository (typically ~/.ivy2/local). Note that you will need to repeat this each time you modify the otherlib sources.

After the project's published locally to the local Ivy repository, you can specify otherlib as a dependency in your SBT project, using the regular SBT dependency for the original version of the forked library (assuming that you haven't changed its ID, version, group ID, etc.). For example, by adding:

libraryDependencies += "com.some_company" % "otherlib" % "1.0.0"

to your build.sbt file.

Now, when you build your project, it will find otherlib in your local Ivy repository (as if it'd been pulled down from a regular repository) and will use your custom version of it.

If otherlib doesn't support SBT, or isn't configured to publish to a repository, and you do not want to modify it to do so, then you can simply copy its .jar file(s) to the /lib directory (~/code/scala/myProgram/lib) of your project.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Mike Allen
  • 8,139
  • 2
  • 24
  • 46
  • Thank you for the answer, surprisingly I couldn't find anything like this online. Cheers – Emil Mar 16 '14 at 04:22
  • This is something really great about SBT. I had to use it specially while using Apache Spark for the dependencies were so big that every time I did a spark project I had to download everything to test it. – Chetan Bhasin Jan 05 '15 at 03:47
9

SBT supports git repositories out of the box. The support is for clone and checkout. See my answer to Can SBT refresh git uri dependency (always or on demand)? or Using Git local repository as dependency in SBT project?, that boil down to the following in build.sbt:

lazy val gitRepo = "git:file:///Users/jacek/sandbox/so/sbt-git/git-repo/#master"

lazy val g = RootProject(uri(gitRepo))

lazy val root = project in file(".") dependsOn g

Once you define the dependency (between projects) you can use it - the git-hosted project - with no other configuration.

Jus12
  • 17,824
  • 28
  • 99
  • 157
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
  • 2
    Handy?! You must be kidding. It's a killer feature of sbt! You should consider using it more often to appreciate its beauty. Wish there were better support for git projects, esp. for `update`. – Jacek Laskowski Mar 16 '14 at 23:06