Scripts Groovy et GShell
Apprenez à écrire et à exécuter des scripts Groovy, ainsi qu’à interagir avec la console Groovy (GroovyShell).
Scripts Groovy et GShell est une leçon Groovy & Gradle: JVM Automation and Build Engineering gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Groovy & Gradle: JVM Automation and Build Engineering, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Groovy & Gradle: JVM Automation and Build Engineering comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Scripts Groovy et GShell » est-elle gratuite ?
Oui — le texte complet de « Scripts Groovy et GShell » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Groovy & Gradle: JVM Automation and Build Engineering, passe à CoddyKit PRO. Le cours Groovy & Gradle: JVM Automation and Build Engineering comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Scripts Groovy et GShell » ?
Apprenez à écrire et à exécuter des scripts Groovy, ainsi qu’à interagir avec la console Groovy (GroovyShell). Tu pratiques Groovy & Gradle: JVM Automation and Build Engineering avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Groovy & Gradle: JVM Automation and Build Engineering ?
Aucune expérience préalable n'est requise. Groovy & Gradle: JVM Automation and Build Engineering sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Scripts Groovy et GShell » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Groovy & Gradle: JVM Automation and Build Engineering ?
Oui. Chaque leçon Groovy & Gradle: JVM Automation and Build Engineering inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Introduction à Groovy et à la JVM
- Syntaxe et types de base de Groovy
- Scripts Groovy et GShell
- Chaînes Groovy et GStrings