如何将 Jar 库添加到 IntelliJ Idea SBT Scala 项目?

发布于 2024-09-24 14:56:22 字数 304 浏览 7 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

战皆罪 2024-10-01 14:56:23

只需在 build.sbt 中声明这一点

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

,所需的 jar 将出现在“外部库”>“非托管-jars”>“Full-Jar-Name.jar”中。如果 jar 文件(在提供的路径中)被修改,这也会改变。

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.

送你一个梦 2024-10-01 14:56:23

在Intellij Idea中:

  • 文件>项目结构> Netbeans 中的库

  • 文件 >项目属性> Eclipse 中的库

  • 右键单击项目 >属性> Java 构建路径 >图书馆

In Intellij Idea:

  • File > Project Structure > Libraries

In Netbeans:

  • File > Project Properties > Libraries

In Eclipse:

  • Right click the project > Properties > Java Build Path > Libraries
執念 2024-10-01 14:56:23

对于多模块 SBT 项目(Intellij 2017.3.4、Scala 12.2.4、sbt 1.1.1),接受的解决方案仅在重新启动或项目刷新之前有效。事实上,“项目设置 -> 模块 -> 依赖项”,然后“+”和“JAR 或目录”会给出警告“模块 X 是从 Sbt 导入的。重新导入后,对其配置所做的任何更改都可能会丢失”。

可能的解决方法
@zero 的建议对我有用,如下:

  1. 将 JAR 放入项目的
    lib 目录。

  2. build.sbtlazy var baseSettings = Seq( ... ) 内添加以下行
    编译中的unmanagementJars += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar")

  3. 还是没有运气吗?在 SBT 工具窗口中,在 unmanagementBaseunmanagementSourceDirectories(等等)下的模块的 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:

  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.

美羊羊 2024-10-01 14:56:23

在项目根路径中创建一个 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

甚是思念 2024-10-01 14:56:23

转到文件 ->项目结构->模块

在左侧窗格中选择模块并转到“依赖项”选项卡

单击 + 添加 JAR 或目录

Go to File -> Project Structure -> Modules

Select module in left pane and go to Dependencies tab

Click + to add JARs or directories

梦幻的味道 2024-10-01 14:56:23

我们可以做的一种方法是在 Intellij 中显式添加 jar,但这种方法的问题是每次构建项目时都需要再次添加它。

第一种方法是:

在 Intellij Idea 中:

File >项目结构>库

另一种方法是在本地 build.sbt 中发布 jar,

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.

如下所示:

organization := "com.abc.core"
name := "abc-test-logging"
version := "1.0-SNAPSHOT"
crossPaths := false
packageBin in Compile := file(s"${name.value}-${version.value}.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

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")
雪落纷纷 2024-10-01 14:56:22

在项目的 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.

最好是你 2024-10-01 14:56:22

更好的方法是将非托管依赖项添加到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文