有时在其他语言中称为字典或关联数组,Groovy 特征映射。映射将键与值关联起来,用冒号分隔键和值,每个键/值对用逗号,整个键和值用方括号括起来。
映射map types = ["maven":"mvn"]
// 定义map
def mytools = [ "mvn": "/usr/local/maven",
"gradle": "/usr/local/gradle" ]
// 根据key获取value
println(mytools["mvn"])
println(mytools["gradle"])
// 根据key重新赋值
mytools["mvn"] = "/opt/local/maven"
println(mytools)
mytools.gradle = "/opt/local/gradle"
println(mytools)
// 获取key的value
println(mytools.key("mvn"))
println(mytools.get("mvn"))
// 判断map是否包含某个key或者value
println(mytools.containsKey("gradle"))
println(mytools.containsValue("/usr/local/gradle"))
// 返回map的key 列表
println(mytools.keySet())
// 根据key删除元素
println(mytools.remove("mvn"))
println(mytools)
map实际使用
map命令工作当中的使用,使用put来用来存放一些变量
def map = [:]
map.put('GIT_URL',"http://192.168.0.55/optimization/data-bigscreen.git")
// 需要修改此处,定义项目名称
map.put('BRANCH',env.branch)
map.put('PACKAGE_TYPE','jar')
map.put('BUILD_COMMAND',"""
cd bigScreenInf
mvn clean install -Dmaven.test.skip=true -U -P test12
""")
map.put('DEPLOY_COMMAND','''
PACKAGE_NAME=`find ./bigScreenInf/ -type f -name *.jar`
ansible -i 192.168.12.111, all -u www -m shell -a "rm -rf /application/java_7099/*.jar"
ansible -i 192.168.12.111, all -u www -m copy -a "src=${PACKAGE_NAME} dest=/application/java_7099/"
ansible -i 192.168.12.111, all -u www -m shell -a "cd /application/java_7099/ && sh ./restart.sh bigScreenInf-0.0.1-SNAPSHOT "
''')
environment {
GIT_URL = "${map.GIT_URL}"
BRANCH = "${map.BRANCH}"
BUILD_COMMAND = "${map.BUILD_COMMAND}"
DEPLOY_COMMAND = "${map.DEPLOY_COMMAND}"
}