官术网_书友最值得收藏!

Executing Groovy code from the command line

Groovy, by definition, is a language with scripting features. Many developers approach Groovy by writing short scripts to automate repetitive tasks. The language provides a set of command-line tools that help you create scripts that are usable within your favorite shell.

In this recipe, we will cover the execution of a simple script with the help of the groovy command, which is made available to you after a successful Groovy installation (see the Installing Groovy on Windows recipe and Installing Groovy on Linux and OS X recipe).

How to do it...

Let's start with the most abused example in programming books, printing Hello, World!:

  1. The simplest way to execute Groovy code is by using the -e option and starting to write Groovy code on the same line:
    groovy -e "println 'Hello, World!'"
    
  2. You can also place the println 'Hello, World!' statement in a separate file; for example, hello.groovy, and execute that script with the following simple command:
    groovy hello.groovy
    
  3. In both cases, you'll see the same results:
    Hello, World!
    

How it works...

In step 1, the actual Groovy code resides in the double quotes (") and uses the predefined println method to print a Hello, World! string. But to explain where the println method actually comes from, we need to give a bit more details on Groovy internals.

Every script in Groovy (a command-line parameter or a standalone script file) is compiled on the fly into a class that extends the groovy.lang.Script class (Javadoc for this class can be found at http://groovy.codehaus.org/api/groovy/lang/Script.html).

Naturally, a Script class is eventually inherited from java.lang.Object, which is the base class for all classes in both Java and Groovy. But since Groovy adds its own extension methods to many standard JDK classes (see the Adding a functionality to the existing Java/Groovy classes recipe in Chapter 3, Using Groovy Language Features for Information on Custom Extension Modules), java.lang.Object is enriched with many useful methods including println (the relevant Java documentation can be found at http://groovy.codehaus.org/groovy-jdk/java/lang/Object.html#println(java.lang.Object)).

There's more...

In fact, the groovy command has several other useful command-line options. If you type groovy --help, you can get a full list of them as shown in the following screenshot:

Let's go through some of those options to get a better overview of the possibilities.

First of all, -classpath, --classpath, and -cp options work in a very similar way to the java command. You just specify a list of the *.jar files or list of directories with the *.class files. The only peculiarity is that -classpath must come as the first parameter in the command line; otherwise Groovy will not recognize it.

Another parameter that is common with the java command is -D, which allows to pass the system properties to your script in the following way:

groovy -Dmessage=world-e "println 'Hello, ' + System.getProperty('message')"

One of the strengths of Groovy (as opposed to Java) is its conciseness. This rule is also applied to what Groovy prints out if an exception occurs in your script:

groovy -e "throw new Exception()"
Caught: java.lang.Exception
java.lang.Exception at script_from_command_line.run(script_from_command_line:1)

To print the conventional full Java stack trace, you can use the -d or -debug options (some stack trace lines are omitted for brevity):

groovy -d -e "throw new Exception()"
Caught: java.lang.Exception
java.lang.Exception
 at sun.reflect.Native...
 ...
 at script_from_command_line.run(script_from_command_line:1)
 ...
 at org.codehaus.groovy.tools.GroovyStarter.main(...)

See also

For additional command-line features, please refer to the following recipes:

  • Using Groovy as a command-line text file editor
  • Using Groovy to start a server on the command line

For more information on the Groovy script structure and Groovy additions, go to:

主站蜘蛛池模板: 合肥市| 公安县| 德州市| 台安县| 望谟县| 加查县| 察隅县| 沙雅县| 灵丘县| 时尚| 吉木萨尔县| 定州市| 澄江县| 隆回县| 鹿泉市| 固安县| 静海县| 阿克| 永川市| 吉林省| 奉新县| 柳林县| 怀集县| 凉城县| 新邵县| 财经| 萝北县| 延津县| 富民县| 成安县| 巢湖市| 平度市| 林周县| 翼城县| 安福县| 衡水市| 伊金霍洛旗| 盐源县| 宜兰县| 富蕴县| 综艺|