GroovyスクリプトとGShell
Groovyスクリプトの記述・実行方法と、Groovyコンソール(GroovyShell)の操作方法を学びます。
「GroovyスクリプトとGShell」はCoddyKit上の無料Groovy & Gradle: JVM Automation and Build Engineeringレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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