简体   繁体   中英

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.

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.

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.

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.

In Intellij Idea:

  • File > Project Structure > Libraries

In Netbeans:

  • File > Project Properties > Libraries

In Eclipse:

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

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.

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

Go to File -> Project Structure -> Modules

Select module in left pane and go to Dependencies tab

Click + to add JARs or directories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM