Ant / XML escaping a standalone Quotation Mark in a property value

Viewed 710

In my ant build-file there is a property:

<property name="pwd" value="" description="value set external"/>

This property is set via GUI by the user. Later I want to call a java task and put the value of the property "pwd" as an argument to the java class:

<java classname="package.and.classname" dir="${directory}" fork="yes" failonerror="true">
     <arg line="-u ${user}"/> 
     <arg line="-p ${pwd}"/> 
     <classpath>
        <pathelement path="[...]"/>            
     </classpath>
  </java>

This works fine until the user has a password containing a single quotation mark (') or a single double quotation mark (").

unbalanced quotes in -p pass'word

Okay - easy workaround for that:

     <arg line="-p &quot;${pwd}&quot;"/> 

But the next case is that pwd = pass"word. The error then: "The syntax of the command is incorrect." And what will happen when both (' and ") are used?

I tried to escape the quotes with &quot; and/or &apos; but in the end nothing does really change in the behaviour. Even if i exchange <arg line> with <arg value> for each parameter the problem still persists (but in fact now only for the double quotation mark or in the mixed case).

For example:

<arg value="-u"/>
<arg value="${user}"/>
<arg value="-p"/>
<arg value="${pwd}"/> 

can work with pwd=pass'word but not with pwd=pass"word or pwd=pass&quot;word.

Is there any way to tell ant that the value of pwd shouldn't be evaluated?

1 Answers

Simply use single quotationmarks for the attributevalue with ", f.e.:

<project> 
   <property name="foo" value='b"ar'/>
    <echo>$${foo} => ${foo}</echo>
</project>

output

[echo] ${foo} => b"ar

EDIT

With mixed case

<project> 
   <property name="foo" value="b&quot;a&apos;r"/>
   <echo>$${foo} => ${foo}</echo>
</project>

output

[echo] ${foo} => b"a'r

Seems ant is able to handle this mixed case, but your Java class isn't.

EDIT

With a class like that

public class Foobar
{
  public static void main(String[] args)
  {
    System.out.println("args[0] => " + args[0] );
    System.out.println("args[1] => " + args[1] );
    System.out.println("args[2] => " + args[2] );
  }
}

and this ant script:

<project>   
 <java classname="Foobar" classpath=".">
   <arg value="b&quot;a&apos;r"/> 
   <arg value="ba&apos;r"/>
   <arg value="b&quot;ar"/>
 </java>  
</project>

all works as expected, output:

 [java] args[0] => b"a'r
 [java] args[1] => ba'r
 [java] args[2] => b"ar

Note = it works only with arg value, f.e.:

<project>   
 <java classname="Foobar" classpath=".">
   <arg value="-p b&quot;a&apos;r"/> 
   <arg value="-p ba&apos;r"/>
   <arg value="-p b&quot;ar"/>
 </java>  
</project>

output

 [java] args[0] => -p b"a'r
 [java] args[1] => -p ba'r
 [java] args[2] => -p b"ar

whereas

<project> 
 <java classname="Foobar" classpath=".">
   <arg line="-p b&quot;a&apos;r"/> 
   <arg line="-p ba&apos;r"/>
   <arg line="-p b&quot;ar"/>
 </java>
</project>

results in:

unbalanced quotes in -p b"a'r 
Related