GroovyServ — Faster Script Startup for Groovy
In this tutorial, you will learn about GroovyServ. We cover key concepts, practical examples, and best practices to help you master this topic.
GroovyServ is a daemon that reduces Groovy script startup time by keeping the JVM warm across invocations, enabling near-instant script execution for rapid development workflows.
What You'll Learn
- Installing and starting GroovyServ
- How the daemon reduces startup time
- Script execution with groovyserv
- Configuration and troubleshooting
Why It Matters
Groovy's JVM startup overhead (1-3 seconds) adds up when running scripts frequently. Doda Browser developers use GroovyServ to run diagnostic scripts with near-zero startup time during development sessions.
Real-World Use
Rapid script development, test-driven workflows, CI/CD local testing loops, and any scenario where scripts are run repeatedly with short execution times.
flowchart LR
A["groovy Script"] --> B["JVM Start"]
B --> C["Class Loading"]
C --> D["Script Execution"]
A -.-> E["groovyclient"]
E --> F["Daemon JVM"]
F --> D
style A fill:#2563eb,stroke:#2563eb,color:#fff
style B fill:#fca5a5,stroke:#dc2626,color:#7f1d1d
style C fill:#fca5a5,stroke:#dc2626,color:#7f1d1d
style D fill:#dbeafe,stroke:#2563eb,color:#1e40af
style E fill:#86efac,stroke:#16a34a,color:#14532d
style F fill:#86efac,stroke:#16a34a,color:#14532d
Installing GroovyServ
# Via SDKMAN
sdk install groovyserv
# Via Homebrew (macOS)
brew install groovyserv
# Manual download from GitHub
# https://github.com/kobo/groovyserv/releases
Starting and Stopping the Daemon
# Start the daemon
groovyserver start
# Check status
groovyserver status
# Stop the daemon
groovyserver stop
# Restart
groovyserver restart
Running Scripts
# Instead of: groovy script.groovy
groovyclient script.groovy
# With arguments
groovyclient script.groovy arg1 arg2
# First run is slower (JVM starts)
# Subsequent runs are near-instant
Performance Comparison
#!/bin/bash
# Time regular groovy
time for i in {1..5}; do
groovy -e "println 'hello'"
done
# Time groovyserv
time for i in {1..5}; do
groovyclient -e "println 'hello'"
done
GroovyServ typically reduces startup from ~1.5s to ~0.1s per invocation.
Daemon Configuration
Configuration file at ~/.groovy/groovyserv.properties:
# JVM arguments for the daemon
groovyserv.jvmargs=-Xmx512m -XX:+UseG1GC -Dfile.encoding=UTF-8
# Port (default: 1961)
groovyserv.port=1961
# Connection timeout in seconds
groovyserv.timeout=30
# Max concurrent clients
groovyserv.max_clients=10
Client Options
# Run expression
groovyclient -e "println 'hello'"
# Run with classpath
groovyclient -cp "lib/*" script.groovy
# Debug mode
groovyclient --debug script.groovy
# Override JVM args
groovyclient -J-Xmx256m script.groovy
How It Works
// The daemon keeps a warm JVM
// Script compilation still happens each time
// But class loading and JIT warmup persist
println "Script ran at: ${new Date()}"
println "Available processors: ${Runtime.runtime.availableProcessors()}"
The daemon compiles each script fresh but reuses the JVM instance.
Security Considerations
# Bind to localhost only (default)
groovyserv.properties:
groovyserv.host=127.0.0.1
# Use SSL for remote connections
groovyserv.ssl=true
groovyserv.keystore=/path/to/keystore
Do not expose GroovyServ to untrusted networks without authentication.
Common Mistakes
1. Forgetting to start the daemon
groovyclient fails if the daemon is not running. Start with groovyserver start.
2. Port conflicts
If another service uses port 1961, change the port in groovyserv.properties.
3. Out-of-memory errors
Scripts that leak memory accumulate in the daemon JVM. Set groovyserv.jvmargs=-Xmx512m and restart regularly.
4. Using groovyclient in CI
CI environments typically start fresh. The startup benefit is minimal. Use regular groovy in CI.
5. Scripts that depend on system properties set before JVM start
Properties set in the client shell are not propagated to the daemon. Pass them with -J-Dproperty=value.
Practice Questions
1. How does GroovyServ achieve faster startup?
It keeps a persistent JVM daemon running, avoiding JVM initialization overhead for each script invocation.
2. What is the default port for GroovyServ?
3. How do you pass JVM arguments to the daemon?
Via groovyserv.jvmargs in groovyserv.properties or -J prefix on the client command line.
4. What happens if the daemon crashes?
Clients get a connection error. Restart the daemon with groovyserver start.
Challenge: Create an alias to automatically use groovyclient for all .groovy file executions.
FAQ
{{< faq question="Is GroovyServ still maintained?" >}} Yes, the project is actively maintained on GitHub by the Kobo team and has Groovy 4 support. {{< /faq >}}
{{< faq question="Can I use GroovyServ with Gradle scripts?" >}} No. Gradle manages its own daemon. GroovyServ is for standalone script execution. {{< /faq >}}
{{< faq question="Does GroovyServ work on Windows?" >}}
Yes, through the groovyclient.bat wrapper.
{{< /faq >}}
{{< faq question="How much memory does the daemon use?" >}}
The daemon typically uses 50-100 MB idle. Configure with -Xmx to limit.
{{< /faq >}}
{{< faq question="Can multiple users share one daemon?" >}} Not recommended. Each user should run their own daemon for isolation. {{< /faq >}}
Mini Project
Create a wrapper script that manages GroovyServ lifecycle and runs scripts with performance metrics.
def serverPid = "pgrep -f groovyserver".execute().text.trim()
if (!serverPid) {
println "Starting GroovyServ..."
"groovyserver start".execute()
Thread.sleep(2000)
}
def scriptFile = args[0]
def start = System.nanoTime()
def proc = "groovyclient $scriptFile".execute()
proc.waitFor()
def elapsed = (System.nanoTime() - start) / 1_000_000
println "Script completed in ${elapsed}ms"
println "Exit code: ${proc.exitValue()}"
if (proc.exitValue() != 0) {
System.err.println proc.err.text
}
What's Next
Now that you understand GroovyServ, proceed to Groovy classes from Java.
| Topic | Description | Link |
|---|---|---|
| Groovy classes | Using Groovy from Java | {{< ref "29-groovy-classes" >}} |
| Scripting | Script development | {{< ref "25-scripting" >}} |
| Grape | Dependency management | {{< ref "27-grape" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro