Scripts de Groovy y GShell
Aprenda a escribir y ejecutar scripts de Groovy e interactuar con la consola de Groovy (GroovyShell).
Scripts de Groovy y GShell es una lección gratuita de Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Groovy & Gradle: JVM Automation and Build Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Groovy Scripts: Quick Automation
A Groovy script is just a file of code that runs directly — no main method required. Perfect for automation and quick utilities.
Simple Script Structure
A Groovy script needs no class or method wrapper — the file itself is the entry point. Just write your lines and run them.
println "Hello from a Groovy script!"
def name = "Coddy"
println "My name is $name"Executing Groovy Scripts
Save your code with a .groovy extension, then run it from the terminal with groovy myScript.groovy. That is the whole workflow.
Script Example: Basic Calculation
This script adds two numbers and branches with an if — a quick taste of how natural and readable Groovy scripting feels.
def num1 = 10
def num2 = 5
def sum = num1 + num2
println "The sum of $num1 and $num2 is: $sum"
if (sum > 10) {
println "That's a big sum!"
}Getting User Input
Scripts can talk to the user too. Read a line from the console with System.in.newReader().readLine(), as shown below.
print "What's your name? "
def name = System.in.newReader().readLine()
println "Hello, $name! Welcome to Groovy."Meet the GroovyShell (GShel)
The GroovyShell (groovysh) is an interactive console — run snippets instantly, test ideas, and get feedback without saving a file.
Launching GShell
Type groovysh to launch the interactive shell, or groovyConsole for the GUI version. We will stick with the command-line one here.
Your First GShell Commands
Inside groovysh, type any Groovy and it runs immediately, showing the result after ===>. Type :exit when you are done.
GShell: Beyond Code
GShell has colon commands for managing your session: :help, :history, :clear, and :reset to wipe declared variables.
Instant Code Testing
GShell shines for instant experiments — call a method or transform a list and see the result right away, no file, compile, or run step.
Script vs. GShell
Consider the following Groovy code snippet:
def x = 5
println x * 2Where would you typically execute this snippet if you want to save it for later use and run it repeatedly?
Recap: Scripts & GShell
You learned the two core Groovy tools: scripts for repeatable .groovy files, and the GroovyShell for instant experimentation.
Preguntas frecuentes
¿La lección «Scripts de Groovy y GShell» es gratis?
Sí — el texto completo de «Scripts de Groovy y GShell» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Groovy & Gradle: JVM Automation and Build Engineering, actualiza a CoddyKit PRO. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.
¿Qué aprenderé en «Scripts de Groovy y GShell»?
Aprenda a escribir y ejecutar scripts de Groovy e interactuar con la consola de Groovy (GroovyShell). Practicas Groovy & Gradle: JVM Automation and Build Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Groovy & Gradle: JVM Automation and Build Engineering?
No se requiere experiencia previa. Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Scripts de Groovy y GShell»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Groovy & Gradle: JVM Automation and Build Engineering?
Sí. Cada lección de Groovy & Gradle: JVM Automation and Build Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Introducción a Groovy y la JVM
- Sintaxis y tipos básicos de Groovy
- Scripts de Groovy y GShell
- Cadenas y GStrings de Groovy