74

I have a library compiled to a jar (not an sbt project, just the jar file) that's not available on a repository.

Is there a simple way to add a reference to the jar in the filesystem/project directly?

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

3 Answers3

71

You can put the jar in your project's lib folder (create it if it doesn't exist), it will then appear on your project's unmanaged-classpath.

To publish a jar file locally, if you have an sbt project that produces the jar, it should be as simple as invoking "publish-local" to publish the jar to your local ivy repository so that you can use that jar in another one of your projects (on the same computer).

Fred Dubois
  • 1,594
  • 13
  • 14
  • Thanks Fred! Do you by chance know the answer to the first question (how to publish a jar file locally?) – Jeff Axelrod Oct 12 '11 at 00:32
  • Updated my answer, sorry about the short original answer, wrote that on my phone while away from my computer :) – Fred Dubois Oct 12 '11 at 00:55
  • 5
    Sorry, I wasn't clear. I was already aware of the publish-local command, but I only have the jar file, not an sbt project. Do you know of a way to accomplish the same thing with just a jar file? – Jeff Axelrod Oct 12 '11 at 14:14
  • 2
    Last comment: You only have to create a folder called lib in the project, and put the jar there. That's all. Additionally, to make it work in eclipse, for example, you have to add the jar to the classpath. – User Apr 02 '13 at 17:28
  • Just to be clear - we add the lib folder under /main/scala, not /main or /main/scala-, correct? – user2029783 Jan 17 '17 at 14:25
  • `publish-local` seems to now be `publishLocal`, so the command `sbt publishLocal` did what I needed, cheers – Anake Apr 29 '21 at 07:17
44

Your SBT project should be structured like this:

README.md
build.sbt
project/
src/
target/

Create a lib/ directory to add a JAR file (e.g. spark-daria_2.11-0.2.0.jar) to the project:

README.md
build.sbt
lib/
  spark-daria_2.11-0.2.0.jar
project/
src/
target/

The location of the lib/ directory should line-up with the output of the sbt "show unmanagedBase" command.

Refresh the project in your IDE and import the code just like you would import an external dependency.

import com.github.mrpowers.spark.daria.sql.DataFrameValidator
Powers
  • 18,150
  • 10
  • 103
  • 108
3

If you have multi-module project you should:

  1. add lib to module dir (not to root). E.g., if you have module core, you should add jar to core/lib.
  2. remove explicit dependency for specified jar in your build.sbt (or in another place). E.g., remove libraryDependencies += <your jar in lib>
Mikhail Ionkin
  • 568
  • 4
  • 20
  • About removing the libraryDependencies, does including "provided" instead of removing it cause the same effect? – Marcos Felipe Feb 06 '23 at 10:13
  • 1
    I don't checked it, but it may be. I think in case of `provided` your assembly will not include libs from `lib` directory, which commonly, IMHO, is not good. – Mikhail Ionkin Feb 06 '23 at 12:25