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

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

问题描述

我想在 Ant 中调用 任务,并需要将路径传递给各种 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 手册:

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>

引用整个参数是行不通的,单个路径需要单独引用.我正在修改的 ant 文件使用属性将各种 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>

我只是将属性修改为:

<property name="libraryjars.unquoted" refid="some.classpath"/>
<property name="libraryjars" value="'${libraryjars.unquoted}'"/>

但这仍然很脆弱,不是吗?有没有更好的办法?如果我有一个带有path1;path2"的属性,我想拆分路径组件,分别引用它们并重新创建属性.我知道如何在 shell 脚本中做到这一点,但 ant 语法对我来说更加神秘:-) 哦,当然它需要在所有平台上工作(至少是 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.

[更新] 感谢@martin 的回答,我找到了使用 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:\path\jar 1;C:\my path\jar2;C:\path\jar3 转换为 "C:\path\jar 1";"C:\my path\jar2";C:\path\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.

推荐答案

一种选择是使用full-XML" proguard 任务,然后每个 jar 将是一个单独的元素,但通常用于渲染属性的路径将使用 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天全站免登陆