[英]Groovy read file line by line and store it into list
I have a file called apps.txt which has three app names in it我有一个名为apps.txt的文件,其中包含三个应用程序名称
frontendapp前端应用
authorizationservice授权服务
connectorservice*连接器服务*
In jenkins pipeline i want to perform some operation on them one by one so i am trying to get them in groovy list using this code -在 jenkins 管道中,我想对它们一一执行一些操作,因此我试图使用此代码将它们放入 groovy 列表中-
list = readFile.readFileLineByLine("${workspace}/apps.txt").collect {it}
for (item in list) {
println "I need to perform some operations on files"
}
But getting groovy.lang.MissingPropertyException.但是得到 groovy.lang.MissingPropertyException。 If i use file class like this - list = new File("${workspace}/apps.txt").collect {it}
then it search for a file on Jenkins master node only and i get fileNotFoundException.如果我像这样使用文件 class - list = new File("${workspace}/apps.txt").collect {it}
然后它仅在 Jenkins 主节点上搜索文件,我得到 fileNotFoundException。
If i use list = readFile("${workspace}/apps.txt").collect {it}
then list gets values character by character.如果我使用list = readFile("${workspace}/apps.txt").collect {it}
然后 list 逐个字符地获取值。 How i can get app names from apps.txt inorder to perform operation on each app.我如何从apps.txt 获取应用程序名称以便对每个应用程序执行操作。
Your attempts are close, but mix things up.你的尝试很接近,但把事情搞混了。
Those are working ways:这些是工作方式:
def list = new File("${workspace}/apps.txt").text.readLines()
Note the .text
call inbetween.注意中间的.text
调用。
def list = readFile("${workspace}/apps.txt").readLines()
Or with the helper Jenkins provides.或者使用 Jenkins 提供的助手。
Side note: .collect{it}
is just .collect()
- and usually is only needed to copy a list.旁注: .collect{it}
只是.collect()
- 通常只需要复制列表。 Since the read lines from the file are already eagerly read, those are copies.由于文件中的读取行已经被急切地读取,因此这些是副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.