Fetching attribute value from property using ant - may be with propertyregex - ant

I tried using linecontainsregexp within filterchain ant-task by framing regexp pattern holding unique server name say 'act1' & now property holds value like this:
<Server name="act1" value="ServerName" port="1234"></Server>
How to get individual attribute names?. For eg if I want to get port #, how to retrieve it. I tried with something like:
<propertyregex property="extracted.prop" input="${server.details}"
regexp="(.*)\\ *##" select="\1" />
thank you.

The code below should work to extract each of the three attributes. First, notice that I'm loading the entire xml file. It isn't necessary to extract the specific line as you were doing. Secondly, I wrote it to be flexible enough to allow line breaks in the Server properties and to allow any order of the attributes.
I see that you were really struggling with the regex in particular. For your understanding, I'll break down the first regex:
(?s) // DOTALL flag. Causes the . wildcard to match newlines.
\x3c // The < character. For reasons I don't understand, `propertyregex` doesn't allow <
Server // Match 'Server' literally
.*? // Reluctantly consume characters until...
name= // 'name=' is reached
" // Because ant is an XML file, we must use this escape sequence for "
(.*?) // Reluctantly grab all characters in a capturing group until...
" // another double quote is reached.
And finally the XML:
<loadfile property="server.details" srcfile="${baseDir}/build/myTest.xml"/>
<propertyregex property="server.name"
input="${server.details}"
regexp="(?s)\x3cServer.*?name="(.*?)""
select="\1" />
<propertyregex property="server.value"
input="${server.details}"
regexp="(?s)\x3cServer.*?value="(.*?)""
select="\1" />
<propertyregex property="server.port"
input="${server.details}"
regexp="(?s)\x3cServer.*?port="(.*?)""
select="\1" />

Related

Is there a way to make #suffix on task basename in Apache Ant case-insensitive?

in my build file, I have declared:
<basename property="filename" file="${args.input}" suffix="XML"/>
where ${args.input} is passed in through an Oxygen transformation scenario, ex -Dargs.input="${cfd}\PMC-min.XML"
${filename} returns PMC-min, which is the desired output. I want the file name without any extension.
However, after discovering an error I realized that
<basename property="filename" file="${args.input}" suffix="xml"/>
was returning PMC-min.XML. So #suffix is case-sensitive.
I could change ${args.input} to ${cfd}\PMC-min (which would require other changes to the build file), or just make sure the extension case of ${args.input} matches #suffix in the scenario. But I was wondering if there was a case-insensitive way to retrieve the filename without the extension in ant? (It doesn't seem to matter if the case of the actual file's extension is different, only the parameters have to match).
One way that might work for you is to use a <mappedresources> "collection of one", something like this:
<mappedresources id="converted">
<string value="${args.input}" />
<chainedmapper>
<flattenmapper />
<globmapper casesensitive="false" from="*.xml" to="*" />
</chainedmapper>
</mappedresources>
<echo message="file_name=${toString:converted}" />
The echo is just to illustrate how to reference the resource.
In the above:
the string resource specifies the input value
the chained mapper combines two mapping steps to alter the string:
a flattenmapper removes the directory part
a globmapper removes the suffix, ignoring case.

how to extract the value of a parameter within a parameter and then print in ant

I want to write a code in such a way that I will remove platform from first 3 lines and then take only the platform name and I will suffix that with installer-zip.${platform_name}.
platform.win-x86=true
platform.win-x64=true
platform.unix=false
installer-zip.win-x86=E:\abc.jar
installer-zip.win-x64=E:\def.jar
Now if the selected item is win-x86 then printing installer-zip.${platform_name} should give me E:\abc.jar. I tried ${installer-zip.${platform_name}} and many other things but they are not working
You cannot do this with regular ant, but you can do this with ant-contrib.
In particular, there is a contrib task property-regex.
So something like:
<propertyregex property="$newProperty"
input="$oldProperty"
regexp="^platform\.(,*)$"
select="\1"
casesensitive="false" />
EDIT: and then...
<property name=desiredProperty value="installer-zip.${newProperty}" />
That should give you enough to work out the exact solution you're looking for...

How to not Escape : Characters in Ant propertyfile task

I have thousands of properties in my property file and I want to change only one property like the following.
<propertyfile file="${mypropetyfile}">
<entry key="jndiname" value="java:comp/env/wm/default"/>
</propertyfile>
but in the property file I am getting the property value with an extra \:
jndiname=java\:comp/env/wm/default
I tried with the <echo> task but it removes other properties. I also tried by concatenation like following in this case also I am getting extra \
<propertyfile file="${mypropetyfile}">
<entry key="jndiname" default="" operation="+" value="java:comp/env/wm/default"/>
</propertyfile>
The \ before the : is an escape character. Although it's not necessary here because the : is not part of the key, but is part of the value, it doesn't hurt either. Using Properties.load() to load this properties file will unescape the :. You should not care about the escape.
Just ran into the same problem changing a property file read by Websphere 6.1 & ended up having to do this workaround:
<property name="jndi.example" value="java:comp/env/example" />
<propertyfile file="jdbc.properties">
<entry key="datasource.example.jndi" operation="=" value="#EXAMPLE"/>
</propertyfile>
<!-- set tokens to property values because ant wants to 'escape the colon' -->
<replace file="jdbc.properties" token="#EXAMPLE" value="${jndi.example}" />
The 'best answer' isn't really addressing the problem. The Properties.load() is not the answer as in the case (which is highly likely), you won't control the 'other side' that will be consuming the properties file.
It doesn't appear you can set the <propertyfile/> to not do this. Seems like a bug to me.
The <replace> suggestion seems like the best course of action imo.
I found that when I used the echo task the entry came out as expected\desired in the file.
However, if I ran the propertyfile task afterwards to populate the same file with some values, it escaped all the colons in the file.
To get around this I simply ensured I ran the propertyfile task first, then the echo.

Ant propertyfile task: how can I set an integer with no thousand separators?

I have a property defined in one of my property files:
<entry key="build" default="0" type="int" operation="+" value="1" />
I read this property using:
<replacefilter token="#build#" property="build_num" />
Once this number gets bigger than 999, thousand separator commas start appearing, like this:
1,001
1,562
Is there a way to get rid of those commas?
(I use build to generate a file name, and don't really want to see any commas in there).
You can prevent thousand separators from being used by adding a pattern to the entry:
<entry key="build" default="0" type="int" operation="+" value="1" pattern="0" />
Note that you'll probably need to manually remove the commas one-time before running this - else your build numbers will reset, with the comma and subsequent digits being discarded. (So 1,325 -> 2 and 4,111 -> 5 and so on.)
Figured ###0 would have worked, but it didn't. Since the project is already making extensive use of ant-contrib, it wasn't difficult to add the regex solution Aaron suggested.
<propertyregex property="build" input="${build}" regexp="," replace="" global="true" override="true"/>

Ant propertyfile replacing value issue

I'm trying to change values in my application.properties file and I'm running into issues with an extra "\" character when trying to substitute url addresses. It doesn't happen when I'm replacing regular text.
Here's the section of the properties file I'm attempting to modify:
# Web Info
web.url=http://www.testaddress.com
web.user=TestAccount
Here's the section of my script that's not working correctly:
<propertyfile file="application.properties">
<entry key="web.url" operation="=" value="${webaddress}" />
<entry key="web.user" operation="=" value="${username}" />
</propertyfile>
What happens is that the web.user is replaced just fine but the address comes out looking like so:
# Web Info
web.url=http\://www.realaddress.com
web.user=RealAccount
I can't account for the backslash, if I echo the ${webaddress} variable it doesn't have it. Any idea as to what may be going on?
Thanks.
Check out the "store" method of the Properties object. The javadoc specifically states:
The key and element characters #, !,
=, and : are written with a preceding backslash to ensure that they are
properly loaded.

Resources