Ammonite REPL: How to import managed dependencies (and JAR files, too)

Ammonite FAQ: How do I load managed dependencies into the Ammonite REPL? What is the syntax for loading dependencies (and JAR files, too)?

Solution

If you want to import a managed dependency into the Ammonite REPL, the solution is to use its import $ivy syntax:

import $ivy.`dev.zio::zio:2.0.22`

Here’s another example of how to import the sttp library:

import $ivy.`com.softwaremill.sttp.client3::core::3.7.2`

This example shows how to import the managed dependency and then import the necessary package to use ScalaTest:

import $ivy.`org.scalatest::scalatest:3.0.8`
import org.scalatest._

Note: You used to be able to use both of those commands on the same line, but that doesn’t seem to work with the current “Ammonite Repl 3.0.0.x”

Using ZIO in the Ammonite REPL

If you want to use ZIO 2 inside the Ammonite REPL, you’ll probably want to use both of these import statements:

import $ivy.`dev.zio::zio:2.0.22`
import $ivy.`fr.janalyse::zio-worksheet:2.0.12.0`

The first one imports the ZIO library, and the second one imports the ZIO Worksheet library, which lets you easily run ZIO applications in a REPL environment, as shown in their example, which I have converted to the Ammonite REPL syntax:

import $ivy.`dev.zio::zio:2.0.22`
import $ivy.`fr.janalyse::zio-worksheet:2.0.12.0`

import zio.*
import zio.worksheet.*

val app = Console.printLine("Hello world !")
app.unsafeRun

In a related note, if you want to use ZIO HTTP and/or ZIO JSON, here are those current import statements:

import $ivy.`dev.zio::zio-http::3.0.0-RC4`
import $ivy.`dev.zio::zio-json::0.6.2`

Related: Loading a JAR file into the Ammonite REPL

In another related note, if you want to load a JAR file into the Ammonite REPL, use this syntax:

import $cp.foo.`simpletest_3.0.0-0.2.0.jar`

That example assumes that you have a JAR file named simpletest_3.0.0-0.2.0.jar in the current directory.

Starting the Ammonite REPL from Scala-CLI

Lastly, if you want to start the Ammonite REPL from Scala-CLI, use this command at your operating system command line:

$ scala-cli repl --amm

In summary, if you want to import managed dependencies into the Ammonite REPL, I hope these examples have been helpful.