ant replaceregexp xml newline - regex

I'm trying to change an xml element value from "true" to "false" using ANT replaceregexp task but am having difficulties matching across a new line. The relevant part of the XML node in question:
<validationRules>
<fullName>CAReversaApprovallLockdown</fullName>
<active>true</active>
In my text editor (sublime), I'm able to use the following regex to find/replace but I can't figure out how to replicate this in ANT replaceregexp:
/fullname>\n <active>true
I can't figure out the correct syntax to match the combination of the newline and the spacing afterwards. The spacing after the newline is always the same, if that makes things easier.
Looking at https://ant.apache.org/manual/Tasks/replaceregexp.html I've tried various combinations of ^ and $ with m flag, \s+ for spaces etc but just can't hit the right combo....any ideas?
My current progress is below but no luck unfortunately...
<target name="deactivate_val_rules">
<echo message="deactivating validation rules..." />
<replaceregexp match="/fullname>\r\n\s+<active>true" flags="gim" byline="false">
<substitution expression="/fullname>\r\n <active>false"/>
<fileset dir="src\objects" includes="Claim_Approvals__c.object"/>
</replaceregexp>
</target>

Got it - the following gave the correct result:
<target name="deactivate_val_rules">
<echo message="deactivating workflows..." />
<replaceregexp match="/fullname>\r\n\s+<active>true" flags="gis" byline="false">
<substitution expression="/fullname>${line.separator} <active>false"/>
<fileset dir="src\objects" includes="Claim_Approvals__c.object"/>
</replaceregexp>
</target>
The output viewed via diff is:
- <fullName>the_name</fullName>
- <active>true</active>
+ <fullName>the_name</fullname>
+ <active>false</active>

To Use replaceregexp you need to define the value to be changed as reference.
For Example:
<validationRules>
<fullName>CAReversaApprovallLockdown</fullName>
<active>true</active>
Ant:
<target name = "deactivate_val_rules">
<echo message="deactivating validation rules..." />
<replaceregexp file="${FILE_LOACTION}/FILE_NAME.FILE_EXT" match="true" replace="false" />
</target>

Related

ANT build : Variable inside resolver:artifacts is undefined

I have setup this ANT build.xml file which pulls in version info from a txt file , reads the first line, trims it and copies it into a variable called 'versionVal' with below code:
<target name="clean"
.
. do something more
.
<loadfile property="versionValTxt" srcfile="version.txt">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.HeadFilter">
<param name="lines" value="1" />
</filterreader>
</filterchain>
</loadfile>
<loadresource property="versionVal">
<propertyresource name="versionValTxt"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="V" to=""/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadresource>
<echo>"Building for version: ${versionVal}"</echo>
</target>
And in one of the targets I am trying to refer a resolver artifact which uses this versionVal to find a file with that specific version in its name as shown below:
<resolver:artifacts id="producedArtifacts" >
<resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
</resolver:artifacts>
<target name="nexus">
<echo>"versionVal: ${versionVal}"</echo>
<resolver:deploy artifactsref="producedArtifacts">
<resolver:remoterepo refid="ossrh"/>
</resolver:deploy>
</target>
And the build keeps failing as below, where it shows the variable versionVal is undefined.
C:\Users\XYZ\git\App\WebContent\dist\App.${versionVal}.zip does not exist
Note that the block is able to recognize ${dist.dir} but it doesnt recognize ${versionVal}. However I am able to print the value using an echo inside the target-nexus.
Much appreciated if anyone can point me in the right direction. I am not able to figure out why this variable is not recognized under "resolver:artifact file" and if there are any alternatives to this problem.
Realised I had to include below resolver artifact inside a target block and then use that as depends in the nexus block. After making this change the variable 'versionVal' was recognized.
Solution:
<target name="packagedArtifact" >
<resolver:artifacts id="producedArtifacts" >
<resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
</resolver:artifacts>
</target>
<target name="nexus" depends="packagedArtifact">
<resolver:deploy artifactsref="producedArtifacts">
<resolver:remoterepo refid="ossrh"/>
</resolver:deploy>
</target>

replaceregexp ant not inserting tabulation

I want to replace a tabulation when I have a match in a file. I have this code:
<property name="line.separator" location="\r" />
<property name="tab.separator" location="\t" />
<target name="replace">
<replaceregexp
match='#WebMethod([\s\S]*?(?=public))public\s+(\w+)\s+(\w*)[\s\S]+?(?=\))[\s\S]+?(?=MSE)(\w+)\s+(\w*)[\s\S+]+?(?=throws)throws\s+(\w*)'
replace='#WebMethod(operationName="\$4")${line.separator}${tab.separator}#RequestWrapper(localName = "\$3")${line.separator}\r#ResponseWrapper(localName = "\$2")${line.separator}\rpublic \$2 \$3\(${line.separator}\r\r\$4 \$5)${line.separator}\r\rthrows MSFWebServiceException' flags="g,m">
<fileset dir="${project.dir}" />
</replaceregexp>
But the part of
#WebMethod(operationName="\$4")${line.separator}${tab.separator}#RequestWrapper
returns this:
#WebMethod(operationName="MSEPDetalleFigPartDTO")
C:t#RequestWrapper
So the \n goes ok, but the \t doesn't work because it replace the \t with C:t instead of a tabulation.
Any help would be appreciate.
kindest regards
This is cause by location instead of value and \t instead of :
<property name="tab.separator" location="\t" />
instead of
<property name="tab.separator" value=" " />
The property line.separator is already set (because it is an Ant built-in property), so your first line is just ignored.
location="\t" means the file location of the file t in the root directory, in your case it is the drive C:

Remove version number from jar in ant task

I'm trying to copy all JARs from one directory to another. During this process I want to remove version numbers at the end of the file names. (E.g. my-jar-1.2.3.jar to my-jar.jar)
I tried to wrap my head around mapper but I can't find a regexp to get that to work. I've tried this:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^.]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
here is a better version of the below answer:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]+.*\.jar$"
to="\1.jar"/>
</copy>
</project>
to handle "That last one is a trouble maker"
This should work properly:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^-]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
THere's a mapper in the maven-ant-task-lib which does just that.
Try this...
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9].*$"
to="\1.jar"/>
</copy>
</project>
In a regex mapper, the from parameter must match the entire name. I use the +? non-greedy pattern matcher. This matches the pattern of . which means any character but not greedily. Normally, this would match the entire line. However, I'm capturing up to the first time a dash followed by a number is found.
The problem happens if the jar has no version number, or it's starts with a non-numeric value. I can successfully, do these:
foo-2.3.2.jar
foo-2r1.jar
But not these:
foo-alpha.jar
foo.jar
So, I tweaked the pattern a bit:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]*.*\.jar$"
to="\1.jar"/>
</copy>
</project>
$ ant
[mkdir] Created dir: lib
[copy] Copying 5 files to lib
[copy] Copying jars/bar-3.4.5.jar to lib/bar.jar
[copy] Copying biff-86.4.2.jar to lib/biff.jar
[copy] Copying jars/boff-2.31.2.jar to lib/boff.jar
[copy] Copying jars/foo-1.2.3.jar tolib/foo.jar
[copy] Copying jars/foo-bar-3.3.2.3.jar to lib/foo.jar
That last one is a trouble maker...
It might be worth doing this in two copies: One to take care if a version number is found, and a second to take care of jars without version numbers.

Ant PropertyRegex won't support capture group when in property

I'm trying to create an ant build target that supports var substitution dynamically.
<target name="replace_property" depends="init_ant_contrib">
<propertyregex input="${replace_inboundproperty"
property="${replace_outboundproperty}"
regex="${replace_match}"
replace="${replace_target}"
global="true"
override="true" />
</target>
so I load the properties file and i'm basically setting the vars as such:
replace_inboundproperty="/target/path/targetfile"
replace_outboundproperty=blah
replace_match="/target/(.*)/targetfile"
replace_target="\1"
so when I echo blah, I'm getting "1". Now if I actually do this:
<target name="replace_property" depends="init_ant_contrib">
<propertyregex input="${replace_inboundproperty"
property="${replace_outboundproperty}"
regex="${replace_match}"
replace="\1"
global="true"
override="true" />
</target>
and echo blah, I'll get "path".
Can anyone tell me what I'm missing to allow the replace to use capture groups from a properties file / ant -D? Using ant-contrib 1.0b3.
Thanks!
Found out that in the properties file, if you double escape it, it'll function correctly:
replace_target=\\1

Ant regexp. Read value of matched expression

I need to replace all occurrence of some regular expression in xml file with proper values loaded from property file. As example
in xml file I have < port=${jnpPort}/>
in property file I have port=3333
I want to have xml file with entries like < port=3333/>
Now using
<replaceregexp match="\$\{(.*)\}" replace="${\1}" flags="g" byline="true">
<fileset dir="." includes="file.xml"/>
</replaceregexp>
I get pretty much the same <port=${jnpPort} />. I would like value of ${jnpPort} were read from property file.
Try:
<replaceregexp match="#< port=\${jnpPort}/>#" replace="#< port=$(port)/>#" flags="g" byline="true">
<fileset dir="." includes="file.xml"/>
</replaceregexp>
You just use copy with a filterset
<filterset id="version.properties.filterset" begintoken="$" endtoken="$">
<filter token="jnpPort" value="${port}" />
</filterset>
<copy file="file.xml.template" tofile="file.xml" overwrite="true" >
<filterset refid="version.properties.filterset" />
</copy>
Okay, not quite copy in place, but pretty good.