Michael
Michael

Reputation: 22947

Ant tokenfilter add string at beginning of each line and remove newlines characters

I need to load properties from a file and change the string the following way :

prop1=value1
prop2=value2

prop3=value3

into :

-Pprop1=value1 -Pprop2=value2 -Pprop3=value3

(Basically I want to add -P in the beginning of each line and remove the newlines)

I have used tokenfilter the following way :

<loadfile property="temp.properties" srcFile="${properties.file}">
    <filterchain>
        <tokenfilter delimoutput=" -P">
            <ignoreblank />
        </tokenfilter>
    </filterchain>
</loadfile>

The problem with this approach is that if, lets say the file ends with empty new lines, I get the following string :

prop1=value1 -Pprop2=value2 -Pprop3=value3 -P
  1. I don't get a -P on the first property (nothing has to do with the newlines at the end of the file). This is easy to overcome with Ant by just concatenating a -P string with the result string
  2. A problem that I need to overcome is the -P at the end. Anyone has a suggestion to fix this ?

Also, a nice addition would be to ignore properties comments - lines that start with # but if it's complicates stuff too much, it's not important.

Thanks

Upvotes: 1

Views: 2441

Answers (1)

devnull
devnull

Reputation: 123508

Does the following work for you?

<filterchain>
  <replaceregex pattern="^[#].*" replace="" />
  <ignoreblank />
  <prefixlines prefix=" -P" />
  <striplinebreaks />
</filterchain>

Upvotes: 3

Related Questions