Instantly Test Any Dependency with Groovy or Scala
Ever needed to try out a quick code snippet with a library from Maven Central? But you REALLY don't want the hassle of creating a whole new dummy Maven/Gradle/sbt project just for a five-minute test? I've been there, and it just doesn't feel great.
Luckily, modern JVM ecosystem gives us dead-simple ways to handle this. Without further ado...
TL;DR
- Prefer Groovy? Use
groovysh
and its built-in dependency manager, Grape, to download and use any library on the fly in a REPL. - Like Scala? Use the
scala-cli
command to launch a REPL with any dependency instantly available.
They both let you bypass the ceremony of a full project setup for quick, exploratory coding.
Either way, don't forget about the TAB completion in the REPL!
The real win is the interactive exploration...it’s a gift for quick exploratory hacks!
Now, say you wanted to play around with the Eclipse JGit library.
The Groovy Way: Grape
The key is Groovy's built-in dependency manager, Grape.
Assuming you have Groovy installed (I highly recommend using SDKMAN!), here’s how you do it:
1️⃣ Fire up the REPL
Pop open your terminal and run groovysh
.
$ groovysh
2️⃣ "Grab" the Dependency
Tell Grape what you want from Maven Central.
// Import the Grape API we need
groovy:000> import static groovy.grape.Grape.grab
// Ask Grape to download the specified artefact and all its dependencies.
// Heads Up! This might take a moment the first time.
groovy:001> grab(group: 'org.eclipse.jgit', module: 'org.eclipse.jgit', version: '7.3.0.202506031305-r')
Grape will fetch JGit and all its transitive dependencies and add them to your classpath. That's it!
3️⃣ Use the Library!
Just import
what you need and use it.
// Import from JGit
groovy:002> import org.eclipse.jgit.api.Git
// Open a git repository
groovy:003> repo = Git.open(new File('/path/to/your/git/repo'))
// Let's get the URL of the first remote
groovy:004> repo.remoteList().call()[0].getURIs()[0]
===> git@github.com:some-username/some-repo.git
The Scala Way: scala-cli
scala-cli offers a similarly powerful way to handle on-the-fly dependencies, with a clean, modern feel.
1️⃣ Fire up the REPL
Pass in the dependency coordinates using the --dependency
parameter.
$ scala-cli --dependency 'org.eclipse.jgit:org.eclipse.jgit:7.3.0.202506031305-r'
2️⃣ Use the Library!
Just import
what you need and use it.
// Import the necessary classes
import org.eclipse.jgit.api.Git
import java.io.File
// Open a git repository
val repo = Git.open(new File("/path/to/your/git/repo"))
// Get the URL of the first remote
val remoteUrl = repo.remoteList().call().get(0).getURIs().get(0)
println(s"Remote URL: $remoteUrl")
Which One To Choose?
Honestly, you can't go wrong with either. Both offer a fantastic interactive experience with auto-complete. The choice really boils down to which language and ecosystem are you most comfortable with?
They are both massive productivity boosters for any developer working on the JVM. Happy hacking!
Comments
Post a Comment