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

查看:96
本文介绍了如何将属性转换为< 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.

例如<property name="arguments" value="foo bar hello world"/>

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

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

我正在使用Ant 1.7.1,所以不能在<exec>任务的<arg>元素上使用前缀"属性(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"直接手动插入属性,然后使用<arg>的"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任务可以在这方面提供帮助:

<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天全站免登陆