Search code examples
regexxmlantazure-devopsazure-pipelines-build-task

Executing Ant 'replaceregexp' OK my local machine, but does not work on Microsoft VSTS


Looking for some help with Ant, and specifically the 'replaceregexp' method. Grateful for any and all advice.

Problem

We are using Ant to deploy XML files, and need to remove certain lines of XML before deploying. We are using Microsoft VSTS as our deployment tool.

We need Ant to scan through a number of files, and delete certain rows. Here is a small example of the source we need to analyse and delete:

<classAccesses>
    <apexClass>CaseEntitlementMilestoneCalc</apexClass>
    <enabled>true</enabled>
</classAccesses>
<classAccesses>
    <apexClass>CaseEntitlementMilestoneCalc_Test</apexClass>
    <enabled>true</enabled>
</classAccesses>   
<classAccesses>
    <apexClass>CaseTriggerHandler</apexClass>
    <enabled>true</enabled>
</classAccesses>

In this example, we want to find and delete this section of XML:

<classAccesses>
    <apexClass>CaseEntitlementMilestoneCalc_Test</apexClass>
    <enabled>true</enabled>
</classAccesses> 

We are using 'replaceregexp' in Ant to do this. Here is the Ant code we have written to do this:

<replaceregexp match="&lt;classAccesses&gt;\n(.*?)&lt;apexClass&gt;CaseEntitlementMilestoneCalc_Test&lt;\/apexClass&gt;\n(.*?)&lt;enabled&gt;(.*?)&lt;\/enabled&gt;\n(.*?)&lt;\/classAccesses&gt;" replace="" flags="gm" byline="false">    
                      <fileset dir="${src.dir}/profiles" includes="**/*.profile" />
                    </replaceregexp>

This works fine when I run it on my Mac via Terminal, but when I attempt to run this on VSTS, it does not do the find/replace. Here are the debug logs for each:

My Mac Debug statement

[replaceregexp] Replacing pattern '<classAccesses>\n(.*?)<apexClass>CaseEntitlementMilestoneCalc_Test<\/apexClass>\n(.*?)<enabled>(.*?)<\/enabled>\n(.*?)<\/classAccesses>' with '' in '/Users/david.morris/DevWorkspace/R_Salesforce_Amey/src/profiles/FSL Mobile User.profile' with flags: 'gm'.
[replaceregexp] Found match; substituting
[replaceregexp] File has changed; saving the updated file

VSTS Debug statement

fileset: Setup scanner in dir d:\a\1\s\src\profiles with patternSet{ includes: [**/*.profile] excludes: [] }
 Replacing pattern '<classAccesses>\n(.*?)<apexClass>CaseEntitlementMilestoneCalc_Test<\/apexClass>\n(.*?)<enabled>(.*?)<\/enabled>\n(.*?)<\/classAccesses>' with '' in 'd:\a\1\s\src\profiles\AAD JIT Provisioning.profile' with flags: 'gm'.
 No change made

Questions

  1. Can anyone see any reason why my Regex might work on my local machine, but not on VSTS?
  2. Are there any other ways that are more fool-proof to do this?

Thank you!


Solution

  • Try to use this code instead:

    &lt;classAccesses&gt;.*&#10;.*&lt;apexClass&gt;CaseEntitlementMilestoneCalc_Test&lt;\/apexClass&gt;.*&#10;.*&lt;enabled&gt;(.*?)&lt;\/enabled&gt;.*&#10;.*&lt;\/classAccesses&gt;