如何正确地引用在Ant任务的路径? [英] How to properly quote a path in an ant task?

查看:202
本文介绍了如何正确地引用在Ant任务的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要叫< proguard相关>在蚂蚁任务和需要的路径传递到不同的JAR文件,例如等价的:

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>

问题是,有些路径可能含有空格或特殊字符,它们必须用引号将这样的的 ProGuard的手动

<proguard>
  -injars /some/path/jar1;"/some other path/jar2"
</proguard>

它不工作引用整个参数,的个人的路径需要单独报价。蚂蚁文件我修改使用属性来传递各种JAR文件路径ProGuard,并将我的问题是如何正确地引用了-injars和-libraryjars个别路径。例如:

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

但是这仍然很脆弱,不是吗?有没有更好的办法?我担心的是什么了财产路径1,路径2,我想分裂路径组件,分别引用它们并建立财产。我知道该怎么做,在一个shell脚本,但蚂蚁语法是很多更神秘的对我:-)哦,当然,它需要在所有平台上(当然至少是Windows,Mac和Linux)的工作,处理的事实路径分隔符的变化,但没关系有Ant脚本恒定某处的。

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.

[更新]感谢@马丁的回答,我发现这样做正是我想要使用的与内 pathconvert 映射链:

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

这将转换 C:\\路径\\罐子1; C:\\我的路径\\ jar2; C:\\路径\\ jar3 C :\\路径\\罐子1,C:\\我的路径\\ jar2; C:\\路径\\ jar3 。该路转换调用每个路径映射器链。如果正则表达式匹配的需要,否则它需要的身份。正则表达式简单地说,如果我们发现没有空格,然后通过一些与至少一个空间的东西,围绕着它在双引号。

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.

推荐答案

一种选择是使用一个'全的XML'proguard的任务,那么每个罐子将是一个独立的元件,但在一般用于再现路径属性你将使用Ant pathconvert 任务。例如:

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

请注意添加的开头和结尾的双引号 - 在 pathsep 仅适用路径的元素之间。
然后在方式来使用它你提到的:

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>

这篇关于如何正确地引用在Ant任务的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆