首页 > 解决方案 > Using IntelliJ, how to add dependency in an sbt project

问题描述

I'm new to sbt and I wanted to learn it with a small Scala project in IntelliJ.

I started with the official sbt Getting Started guide, to learn the sbt basics on the console (https://www.scala-sbt.org/1.x/docs/sbt-by-example.html). Following the guide, everything compiles fine.

Then I created an sbt project in IntelliJ, trying to do the same thing there. When I add the org.scalatest depenpency to the build.sbt file the project can no longer compile. The error message is:

module not found: org.scalatest#scalatest_2.13;3.0.5

When I created the fresh sbt project in IntelliJ, first the build.sbt looked something like this:

name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"

Then I added the dependency:

name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test

After reloading the build file and getting the error when compiling, I tried to change the biuld.sbt according to the code which already worked in the sbt Getting Started guide:

ThisBuild / scalaVersion := "2.13.0"
ThisBuild / organization := "me"
ThisBuild / version := "1.0"

lazy val sbtTest = (project in file("."))
  .settings(
    name := "sbtTest",
    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
  )

This has again the same error after reloading and compiling.

My sbt version is 1.2.8.

Is something wrong with my build.sbt? Or is the sbt version too new for IntelliJ? Is IntelliJ probably not the recommended IDE to create Sala sbt projects?

标签: scalaintellij-ideadependenciessbt

解决方案


你的两个结构都可以build.sbt。这件事似乎在版本中。

Scalatest 3.0.5 目前不适用于 Scala 2.13.0。它适用于 Scala 2.13.0-M2。

Scalatest 3.0.8可用于 Scala 2.13.0。

https://mvnrepository.com/artifact/org.scalatest/scalatest

build.sbt在弹出窗口中重新导入 IntelliJ 项目中修复版本后

弹出窗口

或在 sbt 工具窗口中

sbt 工具窗口

或在 sbt 外壳中

sbt 外壳


推荐阅读