如何将属性转换为 <exec> 的多个参数任务 [英] How to transform property into multiple arguments to <exec> task

查看:21
本文介绍了如何将属性转换为 <exec> 的多个参数任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个值的属性,我想为属性中的每个值执行一个带有单独-j"参数的命令.

I have a property that contains multiple values, and I want to execute a command with a separate "-j" argument for each value in the property.

例如

应该执行:mycommand -j foo -j bar -j hello -j world

Should execute: mycommand -j foo -j bar -j hello -j world

我使用的是 Ant 1.7.1,所以我不能在 元素上使用prefix"属性(Ant 1.8). 任务.

I'm using Ant 1.7.1, so I can't use the "prefix" attribute (Ant 1.8) on the <arg> element of an <exec> task.

一种解决方法是手动将-j"直接插入到属性中,然后使用的line"属性:

One workaround is to insert the "-j" directly into the property by hand and then use the "line" attribute of <arg>:

<property name="args" value="-j foo -j bar -j hello -j world"/>
<exec executable="mycommand">
    <arg line="${args}"/>
</exec>

...但我更喜欢让属性是一个没有嵌入参数的简单列表.

...But I prefer to have the property be a simple list without the embedded arguments.

编辑:实际上,我的参数是 XML 文件中的路径,因此更准确的参数列表是:

Edit: Actually, my arguments are paths within an XML file, so a more accurate argument list would be:

<property name="arguments" value="/foo/bar /hello/world /a/very/long/path"/>

我希望该命令随后使用参数执行:-j/foo/bar -j/hello/world -j/a/very/long/path".请注意,即使在 Windows 下,斜杠仍然是正斜杠(这些是命令的参数,而不是文件名).

I would like the command to then execute with arguments: "-j /foo/bar -j /hello/world -j /a/very/long/path". Note that the slashes remain forward slashes even under Windows (these are arguments to a command, not filenames).

推荐答案

您可以为此使用 Ant 资源工具.

You can use Ant resource tools for this.

<property name="arg_list" value="foo bar hello world"/>    
<resources id="arguments">
  <mappedresources>
    <string value="${arg_list}" />
    <filtermapper>
      <replacestring from=" " to=" -j "/>
    </filtermapper>
  </mappedresources>
</resources>
<property name="arguments" value="-j ${toString:arguments}" />

以上将导致属性 arguments 的值为 -j foo -j bar -j hello -j world,然后可以在 中使用exec arg 行.

The above will result in property arguments having the value -j foo -j bar -j hello -j world, which can then be used in the exec arg line.

或者 pathconvert 任务 可以帮助这方面:

Alternatively a pathconvert task can help in this regard:

<property name="arg_list" value="foo bar hello world"/>    
<pathconvert property="arguments" pathsep=" ">
  <chainedmapper>
    <flattenmapper />
    <regexpmapper from="(.*)" to="-j \1" />
  </chainedmapper>
  <filelist files="${arg_list}" />
</pathconvert>

如果您有绝对路径,而不仅仅是列表中的字符串,则删除flattenmapper.

If you have absolute paths, rather than just strings in the list, then remove the flattenmapper.

如果您有相对路径,请将 flattenmapper 行替换为:

If you have relative paths, replace the flattenmapper line with:

<globmapper from="${basedir}/*" to="*" />

防止路径被转换为绝对路径.

to prevent the paths being converted to absolute.

如果您在 Windows 系统上的 arg_list 中有类似 UNIX 的路径,则 pathconvert 的默认设置将不起作用 - 路径将转换为 Windows 样式.相反,要处理列表,请使用:

In the event that you have UNIX-like paths in the arg_list on a Windows system the default settings for pathconvert won't work - the paths get converted to Windows style. Instead, to process the list use:

<pathconvert property="arguments" pathsep=" " targetos="unix">
  <chainedmapper>
    <regexpmapper from="C:(.*)" to="-j \1" />
  </chainedmapper>
  <filelist files="${arg_list}" />
</pathconvert>

注意 targetos 设置和修改后的 regexmapper 参数.

Note the targetos setting and the revised regexmapper from argument.

这篇关于如何将属性转换为 &lt;exec&gt; 的多个参数任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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