如何将 Jar 库添加到 IntelliJ Idea SBT Scala 项目?
我创建了一个 IntelliJ Idea SBT Scala 项目,例如 Heiko Seeberger 的文章 进行了描述。
我想添加一个 Jar 库(joda-time)以在该项目中使用。如何做到这一点?简单地将它们放入项目的 /lib 中并没有帮助。如果我右键单击Idea项目树中的“外部库”,“新建>”是灰色的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只需在 build.sbt 中声明这一点
,所需的 jar 将出现在“外部库”>“非托管-jars”>“Full-Jar-Name.jar”中。如果 jar 文件(在提供的路径中)被修改,这也会改变。
Just Declare this in build.sbt
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.
在Intellij Idea中:
:
:
In Intellij Idea:
In Netbeans:
In Eclipse:
对于多模块 SBT 项目(Intellij 2017.3.4、Scala 12.2.4、sbt 1.1.1),接受的解决方案仅在重新启动或项目刷新之前有效。事实上,“项目设置 -> 模块 -> 依赖项”,然后“+”和“JAR 或目录”会给出警告“模块 X 是从 Sbt 导入的。重新导入后,对其配置所做的任何更改都可能会丢失”。
可能的解决方法:
@zero 的建议对我有用,如下:
将 JAR 放入项目的
lib
目录。在
build.sbt
的lazy var baseSettings = Seq( ... )
内添加以下行编译中的unmanagementJars += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar")
。还是没有运气吗?在 SBT 工具窗口中,在 unmanagementBase、unmanagementSourceDirectories(等等)下的模块的 sbt 设置 中,尝试调用弹出命令“Show值”和“检查”几次。无论如何,它可能会起作用。
摘自尤金·横田对如何将 sbt-assemble 中的非托管 JAR 添加到最终的 fat JAR? 另一个选项(我没有尝试)是向每个选项添加一个单独的
lib
目录所需模块。希望这些步骤能够解决问题或至少有助于调试。
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:
Put the JAR(s) into the project's
lib
directory.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 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.
在项目根路径中创建一个 lib 目录,将 JAR 粘贴到其中,运行 sbt reload,关闭项目并再次打开它。在 IntelliJ 2018.2.4 和 SBT 1.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
转到文件 ->项目结构->模块
在左侧窗格中选择模块并转到“依赖项”选项卡
单击 + 添加 JAR 或目录
Go to File -> Project Structure -> Modules
Select module in left pane and go to Dependencies tab
Click + to add JARs or directories
我们可以做的一种方法是在 Intellij 中显式添加 jar,但这种方法的问题是每次构建项目时都需要再次添加它。
第一种方法是:
在 Intellij Idea 中:
File >项目结构>库
另一种方法是在本地 build.sbt 中发布 jar,
如下所示:
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
build.sbt would look something like this:
在项目的 IntelliJ Idea 窗口中,转到 File >>>项目结构>>图书馆。单击“库”选项后,将显示两个窗格。在最左侧窗格的顶部,单击绿色的“+”按钮。
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.
更好的方法是将非托管依赖项添加到 build.sbt 中,并避免将部分依赖项管理留给 IDE。
请参阅http://www.scala-sbt.org/release/docs/Library-Management。有关如何定义 unmanagedBase 和 unmanagementJars 任务的详细信息,请参见 html。
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.