groovy脚本参数联动

//获取项目名
def listdata=[]
new File("/Users/fu/Desktop/javaproject/mp08/groovytest/1.txt").eachLine("UTF-8") {
listdata.add(it)
}
println(listdata)
def list_data=""
for (int i = 0; i <listdata.size() ; i++) {
if (listdata[i].toString().contains("projectname")){
list_data=listdata[i]
}
}
def str=list_data.toString().split("=")[-1]
def list=str.tokenize(',')
println(list)
return list



'''
更新properties配置文件里面的value值

params:property_file_path 配置文件路径
params:key 需要添加value对应的键名
params:property_new_value 需要新增的value
'''

def Update_setProperty(property_file_path, key, property_new_value) {

File file = new File(property_file_path);

if (!file.exists()) {
println(property_file_path + "配置文件不存在!!!")
}
//先读取properties文件里面对应key的value值
def props = new Properties()
new File(property_file_path).withInputStream { stream ->
props.load(stream)
}
//通过ConfigSlurper object对象来访问
def config = new ConfigSlurper().parse(props)
def listdata = config.get(key)
def list = listdata.toString().tokenize(",")
//更新properties文件里面对应key的value值,key对应oldvalue值(列表数据类型)oldvalue.add(property_new_value);
//判断新增的字符串是否已经存在,如果已经存在就不需要更新
def result = list.indexOf(property_new_value) //如果列表里面有该元素,则返回该元素的索引位置,否则就返回-1
if (result==-1) {
list.add(property_new_value.trim()) //
def newstring = ""
for (int i = 0; i < list.size(); i++) {
newstring += "," + list[i]
}
def sub_new_str = newstring.substring(1)
println("更新后的数据"+sub_new_str)
//更新对应key的value值
InputStream fis = new FileInputStream(file);
Properties prop = new Properties();
prop.load(fis);
prop.setProperty(key, sub_new_str);
OutputStream fos = new FileOutputStream(file);
prop.store(fos, sub_new_str);

} else {
println("数据已经存在不需要更新数据!!!")
}

}

def file_path = "/Users/个人用户/Desktop/javaproject/mp08/groovytest/configure.properties"
Update_setProperty(file_path, "branch", "5.")
posted @ 2021-01-19 21:44  幸福就这么简单  阅读(277)  评论(0)    收藏  举报