Prevent echo in ant input task - ant

How do you prevent ant's input task from echoing/printing in the console?
When requesting input in ant, it echoes the characters as you type. This isn't ideal for password inputs.

I ended up finding a solution.
As of Ant 1.7.1, this can be done by setting the handler to SecureInputHandler, see code below:
<input
message=" [input] password(Appserver):${line.separator}"
addproperty="password">
<handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>
Strangely, when you set the handler to org.apache.tools.ant.input.SecureInputHandler, it doesn't display as it does with other input in that:
It doesn't have " [input]" prepended
Doesn't move the cursor to the next line
As such, I have achieved these 2 by modifying the message, see above.

Related

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

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" />

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 specify files matching *~?

I have the following ant target:
<delete>
<fileset dir="${qnaire_dir}" includes="**/*~" />
</delete>
It does not delete the following files:
./DETAILS~
./qnaire/__init__.py~
./qtest.py~
./README~
What is the correct includes value to match these files?
I found the answer! The syntax I was looking for:
<defaultexcludes remove="**/*~" />
<delete>
<fileset dir="${qnaire_dir}" includes="**/*~" />
</delete>
<defaultexcludes default="true" />
After skimming the ant documentation for the delete task (http://ant.apache.org/manual/Tasks/delete.html) half a dozen times, I finally actually noticed this statement:
If you use this task to delete temporary files created by editors and it doesn't seem to work, read
up on the default exclusion set in Directory-based Tasks, and see the defaultexcludes attribute
below.
So I went and read about defaultexludes. Turns out that by default ant excludes a set of file expressions from all directory based tasks. These are listed here: http://ant.apache.org/manual/dirtasks.html#defaultexcludes
Adding the first defaultexludes element and removing '*~' from the list allows the subsequent delete task to do the right thing. The second defaultexcludes element puts the default exclusion list back in place.
if you need the regex this matches everything with a ~
.*\~

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 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