Groovy

来自linuxsa wiki
跳转到导航 跳转到搜索

install

sudo apt install  groovy groovy-doc  #Groovy Version: 2.4.21 JVM: 11.0.8 Vendor: Debian OS: Linux

https://www.osradar.com/install-apache-groovy-ubuntu-debian/

usage

当然也可以直接创建.groovy文件用命令行直接运行。

Groovy 简明教程

hello world

emacs    e.groovy
class Example {
   static void main(String[] args) {
      // Using a simple println statement to print output to the console
      println('Hello World');
   }
}


groovy  e.groovy

Hello World

注释annotations

多行注释 

多行注释以/*开头,并且能在行的任何位置 。以/*开头,包括新的行,直到第一个*/结束都被认为是注释的部分。多行注释可以放于声明的开始或者声明的中间。

Groovy 注释

call

def call(Map config) 是 Groovy 语言中定义一个名为 call 的函数的语法。下面是对这部分的详细解释:
关键点

    def:这是 Groovy 中定义变量或函数的关键字,表示定义一个新的函数。

    call:这是函数的名称。在 Groovy 中,特别是在 Jenkins Pipeline 中,如果一个类或脚本重写了 call 方法,可以让这个类的实例像函数一样被调用。

    Map config:这是函数的参数,表示传入一个类型为 Map 的参数。Map 是一种键值对集合,可以存储各种配置选项。在这里,config 可能包含多个配置项,比如节点、分支、凭证 ID 等。

用法示例

在 Jenkins Pipeline 中,这样的函数通常会被用来封装一些常见的操作,例如拉取代码、构建、测试等。调用时,可以传入一个 Map,例如:

groovy

call([
    node