Groovy 脚本与 GShell
学习编写和执行 Groovy 脚本,并与 Groovy 控制台(GroovyShell)交互。
Groovy 脚本与 GShell 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「Groovy 脚本与 GShell」课时是免费的吗?
是的 — 「Groovy 脚本与 GShell」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「Groovy 脚本与 GShell」这节课中我会学到什么?
学习编写和执行 Groovy 脚本,并与 Groovy 控制台(GroovyShell)交互。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「Groovy 脚本与 GShell」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Groovy 与 JVM 入门
- Groovy 语法与基本类型
- Groovy 脚本与 GShell
- Groovy 字符串与 GString