如何将 Jar 库添加到 IntelliJ Idea SBT Scala 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3796004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Jar libraries to an IntelliJ Idea SBT Scala project?
提问by Ivan
I've created an IntelliJ Idea SBT Scala project like Heiko Seeberger's articledescribes.
我创建了一个 IntelliJ Idea SBT Scala 项目,如Heiko Seeberger 的文章所述。
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.
我想在这个项目中添加一个 Jar 库(joda-time)。这个怎么做?简单地将它们放入项目的 /lib 没有帮助。如果我在 Idea 的项目树中右键单击“外部库”,“新建 >”是灰色的。
回答by Ivan
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.
在项目的 IntelliJ Idea 窗口中,转到文件 >> 项目结构 >> 库。单击该库选项后,将显示两个窗格。在最左侧窗格的顶部,单击绿色的“+”按钮。
回答by mcyalcin
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.
更好的方法是将非托管依赖项添加到 build.sbt 中,并避免将部分依赖项管理留给 IDE。
Refer to http://www.scala-sbt.org/release/docs/Library-Management.htmlfor details on how to define your unmanagedBase and unmanagedJars tasks.
有关如何定义 unmanagedBase 和 unmanagedJars 任务的详细信息,请参阅http://www.scala-sbt.org/release/docs/Library-Management.html。
回答by zero
Just Declare this in build.sbt
只需在 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.
并且所需的 jar 将出现在 External Library>unmanaged-jars>Full-Jar-Name.jar 中。如果 jar 文件(在提供的路径中)被修改,这也会改变。
回答by svlzx
In Intellij Idea:
在 Intellij 理念中:
- File > Project Structure > Libraries
- 文件 > 项目结构 > 库
In Netbeans:
在 Netbeans 中:
- File > Project Properties > Libraries
- 文件 > 项目属性 > 库
In Eclipse:
在日食中:
- Right click the project > Properties > Java Build Path > Libraries
- 右键单击项目 > 属性 > Java 构建路径 > 库
回答by esc_space
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".
对于多模块 SBT 项目(Intellij 2017.3.4、Scala 12.2.4、sbt 1.1.1),已接受的解决方案仅在重新启动或项目刷新之前有效。实际上,“项目设置-> 模块-> 依赖项”,然后是“+”和“JAR 或目录”会给出警告“模块 X 是从 Sbt 导入的。重新导入后,对其配置所做的任何更改都可能丢失”。
Possible workaround:
The suggestion by @zero worked for me as follows:
可能的解决方法:
@zero 的建议对我有用,如下所示:
- Put the JAR(s) into the project's
libdirectory. - In
build.sbt, insidelazy var baseSettings = Seq( ... )add the lineunmanagedJars in Compile += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar"). Still no luck? In the SBT tool window, in a module's sbt settingsunder 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
libdirectory to each required module.
- 将 JAR(s) 放入项目的
lib目录中。 - 在
build.sbt,里面lazy var baseSettings = Seq( ... )加一行unmanagedJars in Compile += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar")。 还是没有运气?在SBT工具窗口,在一个模块的SBT设置下 unmanagedBase,unmanagedSourceDirectories(等)尝试调用弹出命令“显示值”和“检查”了几次。不知何故,它可能会奏效。
从尤金横田的回答如何将 sbt-assembly 中的非托管 JAR 添加到最终的胖 JAR?另一种选择(我没有尝试过)是
lib为每个必需的模块添加一个单独的目录。
Hopefully, these steps will solve the problem or at least help debugging.
希望这些步骤可以解决问题或至少有助于调试。
回答by Radu Ciobanu
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
在您的项目根路径中创建一个 lib 目录,将您的 JAR 粘贴到其中,运行 sbt reload,关闭项目并再次打开它。在 IntelliJ 2018.2.4 和 SBT 1.0 中对我来说有效

