Issue
I have a Jenkins pipeline script and it has to read the contents from a properties one by one which is having as a key-value pair. I need to split key and value separately. Below are the properties and groovy file which I am using.
To be read file (mypropsfile.properties) :
product1=workspacename1:path1/path2/path3
product2=workspacename2:path1/path2/path3
product3=workspacename3:path1/path2/path3
My groovy file:
stage('readfromfile') {
steps {
script{
def readpropscontent = readProperties file: 'mypropsfile.properties'
echo 'readpropscontent ::: '+readpropscontent
for (String item : readpropscontent) {
echo "item ::: "+item
def readpropscontentfile2 = item.split("=")[0];
echo 'readpropscontentfile2 ::: '+readpropscontentfile2
}
}
}
}
Updated groovy file to split the value:
def readpropscontent = readFile file: 'mypropsfile.properties' echo 'readpropscontent ::: '+readpropscontent
for (String item : readpropscontent.split('\n')) {
echo "item ::: "+item
def PropsFileValue = item.split("=")[1];
echo 'PropsFileValue ::: '+PropsFileValue
for (String splittingparams : PropsFileValue) {
def path1= splittingparams.split(":")[0];
echo 'path1::: '+path1
//def path2= splittingparams.split(":")[1];
//def path3= splittingparams.split("/")[1];
}
when I try to run it in Jenkins pipeline, I face the below issue:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.AbstractMap$SimpleImmutableEntry.split() is applicable for argument types: (java.lang.String) values: [=]
Possible solutions: split(groovy.lang.Closure), wait(), wait(long), sprintf(java.lang.String, [Ljava.lang.Object;), getAt(java.lang.String), print(java.io.PrintWriter)
I don't want to hardcode the key to fetch its value, by reading the file line by line I need to get the key and value. Can someone provide the inputs to resolve this? Thank you !
Solution
The readProperties
method returns a Map of properties. So you don't have to iterate to read them, simply use the key(name of the property) to extract the value for a specific property.
stage('readfromfile') {
steps {
script{
def readpropscontent = readProperties file: 'mypropsfile.properties'
echo 'readpropscontent ::: '+readpropscontent
echo "PROP1 :::: ${readpropscontent['product1']}"
echo "PROP2 :::: ${readpropscontent['product2']}"
echo "PROP3 :::: ${readpropscontent['product3']}"
}
}
}
Update 1 Printing all keys and values from properties.
stage('readfromfile') {
steps {
script{
def readpropscontent = readProperties file: 'mypropsfile.properties'
echo 'readpropscontent ::: '+readpropscontent
readpropscontent.each{ k,v ->
echo "KEY = $k :::: VAL = $v "
}
}
}
}
Update 2
Using readFile
instead of readProperties
steps {
script{
def readpropscontent = readFile file: 'mypropsfile.properties'
echo 'readpropscontent ::: '+readpropscontent
for (String item : readpropscontent.split('\n')) {
echo "item ::: "+item
def readpropscontentfile2 = item.split("=")[0];
echo 'readpropscontentfile2 ::: '+readpropscontentfile2
}
}
}
Answered By - ycr
Answer Checked By - Cary Denson (JavaFixing Admin)