0

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 !

Kivi
  • 485
  • 1
  • 9
  • 26

1 Answers1

0

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
            }
         }                         
 } 
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Thanks for your response. Is there a way like without using the key can we read the properties file by splitting Key and Value ? – Kivi Sep 09 '22 at 12:16
  • @Kivi check the updated answer. I'm not sure what your requirement is. But I added two updates. One to log all the keys and values, and one to resolve your original code. – ycr Sep 09 '22 at 12:34
  • Thanks ycr, its working for me. I have updated the properties file, After getting the value from properties file I need to split "workspacename1" "path" "path2" "path3" separately. For that how can we use a nested for loop ? You can check the above updated script and it doesn't work – Kivi Sep 11 '22 at 06:32