Pass semicolon in Ant as a parameter

2019-02-19 15:11发布

问题:

I want to pass ";;;" string as a string parameter in my Ant task:

<mytask param=";;;"/>

but Ant consider semicolon as a special symbol and raises an error

java.lang.IllegalArgumentException: Illegal group reference

how can I escape ; symbol to pass it to Ant?

p.s. also I found that I can't pass { symbol,
so I wonder what's the common way to escape characters in Ant?
I've tried "$;$;$;" but it's not working for me

UPDATE: sample code:

public class MyTask extends Task {
    private String value;
    public void setValue(String value) {
        this.value = value;
    }

    public void execute() {
        System.out.println(value);
    }
}

and ant task:

<taskdef name="mytask" classpath="build/lib/CustomTasks.jar"
   classname="MyTask"/>
<mytask value=";;;"/>

回答1:

I have not any problem with this sample:

<target name="test_passing_params">
    <antcall target="test_echo">
        <param name="param1" value=";;;"/>
        <param name="param2" value="{"/>
    </antcall>
</target>
<target name="test_echo">
    <echo>param1: ${param1}</echo>
    <echo>param2: ${param2}</echo>
</target>

Output:

echo
param1: ;;;
echo
param2: {

May be problem in implementation of mytask task?



回答2:

to escape semicolon use: &#59;

Refer to for complete list at: http://www.w3schools.com/TAGS/ref_ascii.asp



回答3:

One note on passing special characters to ant in general:

I used to pass a password to ant SQL task but it failed on special characters such as @$#%^&*!

Switching & with &\amp; worked, but the other characters such as $ or # failed.

I ended up with replacing the password variable string directly inside the ant build file (using any search-replace script such as Linux's sed), rather than sending the parameter to the script using -D.

So if possible, don't waste your time on escaping or switching any special character you find - try to use an easier search-replace solution.