如何在ant任务中正确引用路径?

发布于 2024-11-06 10:39:14 字数 1922 浏览 1 评论 0 原文

我想在 Ant 中调用 任务,并需要向其传递各种 JAR 文件的路径,例如相当于:

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

问题是,其中一些路径可能包含空格或特殊字符,它们必须像 proguard手册

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

引用整个参数是行不通的,各个路径需要单独引用。我正在修改的 ant 文件使用属性将各种 JAR 路径传递给 proguard,我的问题是如何正确引用 -injars 和 -libraryjars 的各个路径。示例:

<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 脚本中的某处有一个常量。

[更新] 感谢@martin的回答,我找到了使用 pathconvert 带有内部映射器链< /a>:

<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:\我的路径\jar2”;C:\路径\jar3。路径转换为每个路径调用映射器链。如果正则表达式匹配,则采用该标识,否则采用该标识。正则表达式只是说,如果我们发现没有空格的内容后跟至少有空格的内容,则将其用双引号引起来。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-11-13 10:39:14

一种选择是使用“完整 XML”proguard 任务,然后每个 jar 将是一个单独的元素,但通常为了渲染属性路径,您将使用 Ant pathconvert 任务。例如:

<fileset id="some.classpath" dir=".">
    ...
</fileset>

<pathconvert property="injars.inner" refid="some.classpath" pathsep='"${path.separator}"' />
<property name="injars" value='"${injars.inner}"' />

请注意添加前导和尾随双引号 - pathsep 仅适用于路径的元素之间。
然后按照你提到的方式使用它:

<proguard>
  -injars ${injars}
</proguard>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文