30

I've created an IntelliJ Idea SBT Scala project like Heiko Seeberger's article describes.

I'd like to add a Jar library (joda-time) to use in this project. How to do this? Simply putting them into project's /lib does not help. If I right-click "External libraries" in Idea's project tree, "New >" is greyed.

Ivan
  • 63,011
  • 101
  • 250
  • 382

8 Answers8

35

In the IntelliJ Idea window of your project, got to File >> Project structure >> Libraries. After clicking that Libraries option, two panes will show up. At the top of the left-most pane, click the green "+" button.

JulienD
  • 7,102
  • 9
  • 50
  • 84
Ivan
  • 63,011
  • 101
  • 250
  • 382
14

The better way to do it is to add your unmanaged dependencies to your build.sbt and refrain from leaving part of your dependency management to your IDE.

Refer to http://www.scala-sbt.org/release/docs/Library-Management.html for details on how to define your unmanagedBase and unmanagedJars tasks.

mcyalcin
  • 2,074
  • 1
  • 15
  • 12
  • the lib folder is automatically added to unmanagedJars, is this supported by IDEA? – Edmondo May 28 '16 at 17:17
  • Yes, Idea looks up the libraries noted in unmanagedJars and does all the navigation / highlighting correctly. If you mean the builds, Idea uses Sbt itself (bundled or external) for building Sbt projects, so they need to do nothing extra to support that. – mcyalcin Jun 07 '16 at 18:30
  • Are you sure lib is automatically used by intellij by default? – matanster Aug 01 '19 at 13:46
  • I checked it right now, and the plugin does not automatically add it to the template. The new project template might have changed in the meanwhile (it's quite possible, or maybe the original owner of the question had imported a project of theirs that had a library configuration), or maybe it was this way all along. My 'yes' was about the second part of that question; that is, if you add an unmanagedJars configuration into build.sbt, the IDE will take it into account in its checks / navigation / highlighting. – mcyalcin Aug 07 '19 at 15:39
10

Just Declare this in build.sbt

unmanagedJars in Compile += file(Path.userHome+"/Your-Jar-Path/Full-Jar-Name.jar")

and required jar will appear in External Library>unmanaged-jars>Full-Jar-Name.jar. This will also change if the jar file(in the provided path) is modified.

zero
  • 2,054
  • 2
  • 14
  • 23
6

In Intellij Idea:

  • File > Project Structure > Libraries

In Netbeans:

  • File > Project Properties > Libraries

In Eclipse:

  • Right click the project > Properties > Java Build Path > Libraries
svlzx
  • 192
  • 3
  • 11
2

For a multi-module SBT project (Intellij 2017.3.4, Scala 12.2.4, sbt 1.1.1), the accepted solution only worked until restart or a project refresh. Indeed, "Project Settings-> Modules -> Dependencies", then "+" and "JARs or directories" gives a warning "Module X is imported from Sbt. Any changes made in its configuration may be lost after reimporting".

Possible workaround:
The suggestion by @zero worked for me as follows:

  1. Put the JAR(s) into the project's lib directory.

  2. In build.sbt, inside lazy var baseSettings = Seq( ... ) add the line unmanagedJars in Compile += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar").

  3. Still no luck? In the SBT tool window, in a module's sbt settings under unmanagedBase, unmanagedSourceDirectories (and the like) try calling the pop-up commands "Show value" and "Inspect" a few times. Somehow, it might work.

    From Eugene Yokota's answer to How can I add unmanaged JARs in sbt-assembly to the final fat JAR? another option (which I didn't try) is to add an individual lib directory to each required module.

Hopefully, these steps will solve the problem or at least help debugging.

Community
  • 1
  • 1
schrödingcöder
  • 565
  • 1
  • 9
  • 18
0

Create a lib directory in your project root path, paste your JARs in there, run sbt reload, close the project and open it again. Works for me in IntelliJ 2018.2.4 and SBT 1.0

Radu Ciobanu
  • 710
  • 8
  • 10
0

Go to File -> Project Structure -> Modules

Select module in left pane and go to Dependencies tab

Click + to add JARs or directories

user3755282
  • 813
  • 2
  • 9
  • 15
0

One way we can do is by adding jar explicitly in the Intellij but the problem with this approach is every time we build the project we need to add it again.

First way to do is :

In Intellij Idea:

File > Project Structure > Libraries

Another way is to publish the jar in local

Step 1 : Place the jar in any accessible folder.
Step 2 Create a build.sbt file which contains the detail of the jar.
Step 3 Open cmd to that path and execute command sbt publishLocal.
Step 4 Build your code and you would observe that you are able to access the classes belong to that jar.

build.sbt would look something like this:

organization := "com.abc.core"
name := "abc-test-logging"
version := "1.0-SNAPSHOT"
crossPaths := false
packageBin in Compile := file(s"${name.value}-${version.value}.jar")
sway
  • 361
  • 3
  • 3