Ivan
Ivan

Reputation: 64207

How to add Jar libraries to an IntelliJ Idea SBT Scala project?

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.

Upvotes: 30

Views: 37263

Answers (8)

sway
sway

Reputation: 591

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

Upvotes: 0

user3755282
user3755282

Reputation: 883

Go to File -> Project Structure -> Modules

Select module in left pane and go to Dependencies tab

Click + to add JARs or directories

Upvotes: 0

Radu Ciobanu
Radu Ciobanu

Reputation: 748

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

Upvotes: 0

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.

Upvotes: 2

Ivan
Ivan

Reputation: 64207

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.

Upvotes: 35

zero
zero

Reputation: 2104

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.

Upvotes: 10

mcyalcin
mcyalcin

Reputation: 2082

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.

Upvotes: 14

svlzx
svlzx

Reputation: 192

In Intellij Idea:

  • File > Project Structure > Libraries

In Netbeans:

  • File > Project Properties > Libraries

In Eclipse:

  • Right click the project > Properties > Java Build Path > Libraries

Upvotes: 6

Related Questions