Mot
Mot

Reputation: 29530

ANT: set property using loadfile if certain multi-line pattern is found

I have a source code file which contains, among others, following lines:

   public boolean isBetaVersion() {
      return true;
   }

I try to read this file in ANT and set a property depending on whether the above method returns true or something else:

<loadfile srcfile="path/to/MyFile.java" property="isBetaBuild">
   <filterchain>
      <striplinebreaks/>
      <linecontainsregexp>
          <regexp pattern=".*\sisBetaVersion()\s\{\sreturn\strue.*"/>
      </linecontainsregexp>
   </filterchain>
</loadfile>

but it never matches (unfortunately, I'm a complete regex-noob). Anyone has an idea what could be wrong? Thanks in advance.

Upvotes: 0

Views: 809

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

You need to escape the parentheses, and allow for more than just one whitespace character. Then, \b anchors can be used to match beginnings and ends of words, which is also helpful here. I suggest you try this:

<regexp pattern="\bisBetaVersion\(\)\s+\{\s+return\s+true\b"/>

You don't need the .* around the regex, since you're just looking for whether that substring is contained in the source code somewhere.

Upvotes: 1

Related Questions