问题
I want to call the <proguard>
task in Ant and need to pass it the paths to various JAR files, e.g. the equivalent of:
<proguard>
-injars /some/path/jar1;/some other path/jar2
</proguard>
Problem is, some of these path may contain spaces or special characters, they have to be quoted like this as explained in the proguard manual:
<proguard>
-injars /some/path/jar1;"/some other path/jar2"
</proguard>
It doesn't work to quote the whole argument, the individual paths need to be quoted separately. The ant file I'm modifying uses properties to pass the various JARs paths to proguard and my issue is how to properly quote the individual paths for the -injars and the -libraryjars. Example:
<property name="libraryjars" refid="some.classpath" />
<proguard>
@${proguard.config}
-libraryjars ${libraryjars}
</proguard>
I just modified the property to look like:
<property name="libraryjars.unquoted" refid="some.classpath"/>
<property name="libraryjars" value="'${libraryjars.unquoted}'"/>
but that's still brittle, isn't it? Is there a better way? What about the fact I have a property with "path1;path2", I'd like to split the path components, quote them separately and recreate the property. I know how to do that in a shell script but ant syntax is a lot more mysterious to me :-) Oh and of course it needs to work on all platforms (well at least Windows, Mac and Linux), dealing with the fact the path separator changes, but that's Ok there's a constant somewhere for that in the ant script.
[Update] Thanks for @martin's answer, I found the perfect way to do exactly what I wanted using pathconvert with an inner mapper chain:
<pathconvert property="dest.path" refid="source.path">
<firstmatchmapper>
<regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
<identitymapper/>
</firstmatchmapper>
</pathconvert>
This will convert C:\path\jar 1;C:\my path\jar2;C:\path\jar3
into "C:\path\jar 1";"C:\my path\jar2";C:\path\jar3
. The path conversion calls the mapper chain for each path. If the regexp matches it takes that otherwise it takes the identity. The regexp simply says that if we find something with no space followed by something with at least a space, surround it in double quotes.
回答1:
One option would be to use a 'full-XML' proguard task, then each jar would be a separate element, but in general for rendering paths to properties you would use the Ant pathconvert task. For example:
<fileset id="some.classpath" dir=".">
...
</fileset>
<pathconvert property="injars.inner" refid="some.classpath" pathsep='"${path.separator}"' />
<property name="injars" value='"${injars.inner}"' />
Note the addition of leading and trailing double quotes - the pathsep
only applies between the elements of the path.
Then use it in the way you mention:
<proguard>
-injars ${injars}
</proguard>
来源:https://stackoverflow.com/questions/5955831/how-to-properly-quote-a-path-in-an-ant-task