简体   繁体   中英

How to pass empty value in ant command, so that it replaces property value in build.xml file

I am constructing ant parameters as String array and passing it to ProcessBuilder class(in JAVA) to run it. For eg

String doubleQuotes = isWindows ? "\"" : "";
String[] args = { antHome + "/bin/" + command, "-f", xmlHome + "/path/to/xmlFile.xml",
                "-lib", libHome,
                "-Dusername=" + username,"-Dpassword=" + doubleQuotes + password + doubleQuotes,
                "antTarget" };

And here is XML snippet, where the text is to be replaced:

<target name="antTarget" ....>
...
<![CDATA[

                        <cred>
                            <username>${username}</username>
                            <password>${password}</password>
                        </cred>
]]>
...
</target>

Now, if I don't receive username and password from the caller(as they are optional), I end up having value like:

                        <cred>
                            <username>-Dpassword</username>
                            <password>${password}</password>
                        </cred>

Same code seems to work in Linux, but not in Windows machine.

Enclose the variable within double quotes. For eg:

String doubleQuotes = isWindows ? "\"" : "";
String[] args = { antHome + "/bin/" + command, "-f", xmlHome + "/path/to/xmlFile.xml",
                "-lib", libHome,
                "-Dusername=" + doubleQuotes + username + doubleQuotes,"-Dpassword=" + doubleQuotes + password + doubleQuotes,
                "antTarget" };

Compare the above solution with the one in question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM