Ant regexp. Read value of matched expression - regex

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.

Related

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:

ant replaceregexp xml newline

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>

Ant copy files from dynamic directories further down

I am trying to copy files from some { dynamic } directories X into another location.
Some of the destination directories should be the ( same and static ) as in the source path.
The initial layout looks like this under new directory A/D.
(A)
|_(B)
|_(C)
|_{X}
|_{Y.js}
The final layout should look like this:
A
|_D
|_B
|_C
|_{X}
|_{Y.js}
I tried using a regexmapper, but I am getting messages that entries are skipped because copy doesn't know how to handle it.
<copy toDir="A/D/" verbose="true">
<fileset dir="A/" casesensitive="yes">
<include name="B/C/**/*.js"/>
</fileset>
<regexpmapper from="B/C/([^/]+)/(.+\.js)" to="B/C/\1/\2"/>
</copy>
Edit: I am currently trying on Windows but want this to be portable.
There is no need to use regexmapper and here is the solution I came up with:
<copy toDir="A/D" verbose="true">
<fileset dir="A" casesensitive="no" overwrite="true">
<include name="B/C/**/*.js"/>
</fileset>
</copy>

Copying files matching a regex in ant

I'm having trouble copying some files which match a pattern in my ant script.
I have the following:
<property name="IncludedLocales" value="de_DE|es_ES|fr_FR|it_IT|ja_JP" />
<copy todir="${dest}">
<fileset dir="${src}" includes="**/*.properties" />
<mapper type="regexp" from="(messages(_${IncludedLocales})?\.properties)" to="\1" />
</copy>
${src} contains the files messages.properties, as well as messages_de_DE.properties, messages_es_ES.properties, etc.
But for some reason, only messages.properties, and messages_de_DE.properties get copied (and if I changed IncludedLocales to 'es_ES|de_DE|fr_FR|it_IT|ja_JP', messages_es_ES.properties gets copied instead of messages_de_DE.properties).
Am I missing something really simple?
Yes, I did miss something really simple, (messages(_${IncludedLocales})?\.properties) should have been (messages(_(${IncludedLocales}))?\.properties)

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.