Just-in-time compilation and packaging of groovy scripts

Source: Internet
Author: User
Tags command line groovy script

In this article, you will be involved in:

Use Clibuilder to support command-line options, and the parameters needed to execute the script are passed through command-line options.

Use Groovyclassloader to load Groovy class.

Use Antbuilder to build the Jar package.

Before you start

About this article

Maybe you've written some interesting or useful groovy scripts and want to share them with your friends, but you don't want to force them to install groovy, so you might want to make a gadget that your friends can use to compile your groovy scripts and build an executable Jar package. This article describes how to make a gadget that allows your groovy script to be Just-in-time compiled and packaged into an executable Jar without groovy, and introduces some of the interesting features of groovy in this process.

In this article, we'll design and implement a command-line tool that completes Just-in-time compilation and packaging of Groovy scripts. We'll use Clibuilder to implement the processing of the command line, use Antbuilder to handle packaged issues, and use the Filesystemcompiler class to compile the Groovy script file.


Use this example to understand how clibuilder is used in groovy and how antbuilder is used in groovy.


Eclipse IDE

Groovy Plugin for Eclipse

Apache Ant Java Library (where you can find the download address http://ant.apache.org/bindownload.cgi)

System Requirements

Because both the Eclipse IDE and the Groovy language are cross-platform, you can write the program in this example on any platform and run it on any platform.

Using Clibuilder to design a command-line program

Writing scripts in Groovy is a good way to develop cross-platform tools. As the complexity of the script grows, you may want to handle command-line options in your script. Handling these options or parameters and displaying the appropriate help information depending on the situation can be a hassle. Groovy has bundled the Apache Commons CLI Library as part of it, but it also provides a much simpler clibuilder to use. So then let's see how we can use it.

To set command-line options for a program

Listing 1. To create a Clibuilder instance and define command line options

Create a Clibuilder instance and define command-line options
def cmdline = new Clibuilder (usage: ' Groovypackman-[chflms] [date] [prefix] ') 
Cmdli Ne.h (longopt: "Help", Required:false, ' Show usage information ') 
CMDLINE.D (longopt: ' DestFile ', argname: ' DestFile ') , Required:false, args:1, ' jar 
destintation filename ') 
cmdline.m (longopt: ' MainClass ', argname: ' MainClass ', R Equired:true, Args:1, ' fully 
qualified main class ') 
cmdline.c (longopt: ' Groovyc ', Required:false, ' Run Groovy C ') 
cmdline.s (longopt: ' SourcePath ', argname: ' SourcePath ', required:true, args:1, ' fully 
path to the Mainclas S. Must is specified with. Groovy file path when-c option used. ')

In the preceding code snippet, we first create an instance of Clibuilder. Clibuilder has a property called Usage that you can use to display information such as the instructions for the program. Here, it is passed as a parameter to the constructor to tell the program's users what options they can use. Next, we use the method named one letter to define the command-line option, and the Longopt property allows the option to be specified as either-H or--help. The Argname property is used to specify the parameter name of the option that is displayed in the usage description. The Args property is used to specify the number of parameters required for an option. The Required property is used to tell the command-line parser whether the current option is required. The last parameter in the function is used to specify the descriptive information for this option.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.