1. groovy, the Groovy command
groovy invokes the Groovy command line processor. It allows you to run inline Groovy expressions, and scripts, tests or application within groovy files.
It plays a similar role to java in the Java world but handles inline scripts and rather than invoking class files, it is normally called with scripts
and will automatically call the Groovy compiler as needed.
The easiest way to run a Groovy script, test or application is to run the following command at your shell prompt:
> groovy MyScript.groovy
The .groovy part is optional. The groovy command supports a number of command line switches:
| Short version | Long version | Description | Example |
|---|---|---|---|
-a |
--autosplit <splitPattern> |
split lines using splitPattern (default '\s') using implicit 'split' variable |
|
-b |
--basescript <class> |
Base class name for scripts (must derive from Script) |
|
-c |
--encoding <charset> |
specify the encoding of the files |
|
-cp <path> |
-classpath <path> |
Specify the compilation classpath. Must be the first argument. |
groovy -cp lib/dep.jar MyScript |
--configscript <path> |
Advanced compiler configuration script |
groovy --configscript config/config.groovy src/Person.groovy |
|
-D |
--define <name=value> |
define a system property |
|
-d |
--debug |
debug mode will print out full stack traces |
|
--disableopt <optlist> |
disables one or all optimization elements. |
||
-e <script> |
specify an inline command line script |
groovy -e "println new Date()" |
|
-h |
--help |
Displays usage information for the command line groovy command |
groovy --help |
-i <extension> |
modify files in place; create backup if extension is given (e.g. '.bak') |
||
-l <port> |
listen on a port and process inbound lines (default: 1960) |
||
-n |
process files line by line using implicit 'line' variable |
||
-p |
process files line by line and print result (see also -n) |
||
-v |
--version |
display the Groovy and JVM versions |
groovy -v |
-pa |
--parameters |
Generates metadata for reflection on method parameter names on JDK 8 and above. Defaults to false. |
groovy --parameters Person.groovy |
-pr |
--enable-preview |
Enable preview Java features (jdk12+ only). |
groovy --enable-preview Person.groovy |
1.1. Configuring logging
Groovy’s command-line tools use JDK Platform Logging (System.Logger)
for diagnostic messages. By default, this is backed by java.util.logging (JUL).
1.1.1. Logging configuration file
JUL reads its configuration from exactly one source. The source is resolved in the following order:
-
The file specified by
-Djava.util.logging.config.file=…on the command line (viaJAVA_OPTS). -
The file
~/.groovy/logging.properties, if it exists. Groovy automatically loads this at startup. -
The JDK default at
$JAVA_HOME/conf/logging.properties.
The first source found is used and the rest are ignored.
1.1.2. Example configuration
# ~/.groovy/logging.properties
.level=INFO
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tT %4$s [%3$s] %5$s%6$s%n
# Show Grape provider discovery details
groovy.grape.Grape.level=FINE
1.1.3. Logger reference
The following table lists all classes that emit log messages,
grouped by module. The JUL level column shows the java.util.logging
level to set in logging.properties (the Platform Logging level is shown in parentheses).
Core (groovy)
| Logger name | JUL level | What is logged |
|---|---|---|
|
FINE (DEBUG), WARNING, SEVERE (ERROR) |
GrapeEngine provider discovery, selection, and instantiation failures |
|
SEVERE (ERROR) |
Script execution errors and uncaught exceptions |
|
SEVERE (ERROR) |
Parser errors in the lexer debugging tool |
|
SEVERE (ERROR) |
Stack operation errors during bytecode generation |
|
FINE (DEBUG) |
ASM bytecode disassembly output |
|
FINE (DEBUG), WARNING |
XStream serialization availability and failures |
|
INFO |
DGM method stub generation progress |
|
WARNING, SEVERE (ERROR) |
Compilation errors and warnings |
|
SEVERE (ERROR) |
Startup configuration errors |
|
INFO |
Joint compilation progress |
Grape Ivy (groovy-grape-ivy)
| Logger name | JUL level | What is logged |
|---|---|---|
|
INFO, FINE (DEBUG), FINEST (TRACE) |
Ivy dependency resolution, downloads, and cache operations |
Ant tasks (groovy-ant)
| Logger name | JUL level | What is logged |
|---|---|---|
|
WARNING |
Ant task compilation warnings |
|
WARNING |
Ant task groovydoc warnings |
Console (groovy-console)
| Logger name | JUL level | What is logged |
|---|---|---|
|
WARNING |
Text editor component warnings |
GroovyDoc (groovy-groovydoc)
| Logger name | JUL level | What is logged |
|---|---|---|
|
WARNING |
File output warnings |
|
WARNING |
Doc parsing warnings |
|
WARNING, SEVERE (ERROR) |
Template processing errors |
|
WARNING |
Doc building warnings |
JSON (groovy-json)
| Logger name | JUL level | What is logged |
|---|---|---|
|
SEVERE (ERROR) |
JSON parsing exception details |
|
WARNING |
System property access warnings |
Servlet (groovy-servlet)
| Logger name | JUL level | What is logged |
|---|---|---|
|
SEVERE (ERROR) |
Servlet script execution errors |
Swing (groovy-swing)
| Logger name | JUL level | What is logged |
|---|---|---|
|
WARNING |
Table sorting comparison warnings |
Testing (groovy-test, groovy-test-junit5)
| Logger name | JUL level | What is logged |
|---|---|---|
|
INFO |
Test suite class loading info |
|
WARNING |
JUnit5 runner warnings |
To enable all diagnostic logging, set .level=FINE in your configuration file.
To focus on a specific area, set the logger name to FINE (or FINER/FINEST for trace-level detail)
and leave the root level at INFO or WARNING.
|
If a different System.LoggerFinder is on the classpath (e.g., via SLF4J/Logback or Log4j2),
platform logging calls are routed to that backend instead, and logging.properties has no effect.
Configure the alternative backend using its own mechanism (e.g., logback.xml or log4j2.xml).
|