[workflow-plugin] Reading and parsing property file

515 views
Skip to first unread message

Rupali

unread,
Nov 27, 2014, 7:10:16 AM11/27/14
to jenkins...@googlegroups.com
Hi,

I tried reading file from workspace using readFile and then echo the results. That works just fine. But I was unable to parse it and retrieve specific values. Is there a way to parse the contents of this file and read properties into variables?
File will contain key=value kind of entries.

Regards,
Rupali

James Nord

unread,
Nov 27, 2014, 9:13:21 AM11/27/14
to jenkins...@googlegroups.com
Hi Rupali,

readFile returns a String object with the entire contents of the file - 
so you can parse it using stand java/groovy APIs.

.e.g.
you can chain a StringReader and Properties

https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
http://docs.oracle.com/javase/8/docs/api/java/io/StringReader.html#StringReader-java.lang.String-

pseudo code...

    def str = readFile name: 'somefile', charset : 'utf-8'
    def sr = new StringReader(str)
    def props = new Properties()
    props.load(sr)

    def foobar = props.getProperty('somepropname')


/James
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d50d9908-49e8-478d-8151-f8f19b804790%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rupali

unread,
Nov 28, 2014, 3:57:04 AM11/28/14
to jenkins...@googlegroups.com
Thanks James.
That worked.

Regards,
Rupali

Gary Clayburg

unread,
Jan 15, 2015, 2:13:33 PM1/15/15
to jenkins...@googlegroups.com
FYI - the syntax for readFile() is incorrect. It should be:

        def str = readFile file: 'somefile', encoding : 'utf-8'

Reply all
Reply to author
Forward
0 new messages