将可选路径参数传递给 ant

发布于 2024-11-25 19:44:17 字数 1533 浏览 5 评论 0原文

我有一个 ant 任务,它使用 apply 任务在一组文件上运行脚本。

我有一个类似这样的目录结构: mkdir -pa/{b,c,d,e}/f

通常(如果我不传递任何参数),我希望 ant 在所有 f 上运行。 也就是说,如果我调用 ant mytask,它应该处理:a/b/f、a/c/f、a/d/f、a/e/f 。这已经可以使用 apply 和patternets 来实现。

但是,当我向它传递一个名为 foo 的可选参数时,它应该只调用 a/foo/f 上的脚本。

因此,如果我调用 ant mytask -foo b,它应该只处理 a/b/f,而不处理其他的。

我已阅读这篇文章,它解释了方法传递参数,并且我查看了有关 properties 的 ant 文档,和条件。但我仍然无法以有效的方式将它们拼凑在一起。

另外,我不想使用上面的建议之一,它要求这样的参数:

<arg value="${arg0}"/>
<arg value="${arg1}"/>

我希望能够将其称为 ant mytask -foo valueoffoo 对于任何任意 foo.

谢谢。


我尝试了下面马丁·克莱顿的建议,代码如下:

<target name="mytask">
  <property name="foo" value="*" />
  <apply executable="perl">
    <arg value="somescript"/>
    <dirset id="them" dir="a">
      <include name="${foo}/*/f" />
    </dirset>
  </apply>
</target>

上面的代码满足了我的要求。

注 1:在我的实际代码中,我使用模式集而不是 dirset,但它应该工作相同。

注2:在我原来的问题中,我说目录结构是a/{b,c,d,e}/f。事实上,它有点复杂,因此上面包含中的 * 。我第一次忽略了这一点,因为它似乎不相关。

I have an ant task that uses an apply task to run a script on a group of files.

I have a directory structure resultant of something like this:
mkdir -p a/{b,c,d,e}/f

Normally (if I pass no arguments), I would like ant to run on all fs.
That is, if I called ant mytask, it should process: a/b/f, a/c/f, a/d/f, a/e/f. This already works using apply and patternsets.

However, when I pass it an optional argument called foo, it should only call the script on a/foo/f.

So if I called ant mytask -foo b, it should process a/b/f only, and not the others.

I have read this SO post, which explains ways of passing arguments, and I have looked at the ant documentation regarding properties, and conditionals. But I am still unable to piece them together in a way that works.

Also, I do not want to use one of the suggestions from the SO above which called for arguments like this:

<arg value="${arg0}"/>
<arg value="${arg1}"/>

I want to be able to call it as ant mytask -foo valueoffoo for any arbitrary foo.

Thanks.


I tried martin clayton's suggestion below and have code like:

<target name="mytask">
  <property name="foo" value="*" />
  <apply executable="perl">
    <arg value="somescript"/>
    <dirset id="them" dir="a">
      <include name="${foo}/*/f" />
    </dirset>
  </apply>
</target>

The above does what I want.

Note 1: In my actual code I use a patternset instead of dirset but it should work the same.

Note 2: In my original question I said the directory structure was a/{b,c,d,e}/f. It is in fact a bit more complicated, hence the * in the include above. I omitted that the first time around because it did not seem relevant.

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

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

发布评论

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

评论(1

烟酒忠诚 2024-12-02 19:44:17

您可以执行此操作 - 尽管命令行语法略有不同 -
使用属性“覆盖”。

首先,在构建文件中,从属性 foo 构造文件集或目录集,
像这样的东西:

<property name="foo" value="*" />
<dirset id="them" dir="a">
    <include name="${foo}/f" />
</dirset>

这将为您提供默认行为 - 处理所有
a 的子目录本身有一个子目录 f

现在,如果您像这样运行 Ant:

ant -Dfoo=d

只会处理目录 a/d/f

这是可行的,因为 Ant 属性是不可变的 - 嗯,通常情况下是不可变的 -
因此 foo 的命令行定义会阻止使用构建文件中的定义。

You can do this - albeit with a slightly different command-line syntax -
using a property 'override'.

First, in the buildfile, construct your fileset or dirset from a property foo,
something like this:

<property name="foo" value="*" />
<dirset id="them" dir="a">
    <include name="${foo}/f" />
</dirset>

This will give you your default behaviour - processing all
subdirectories of a that themselves have a subdirectory f.

Now, if you run Ant like this:

ant -Dfoo=d

Only directory a/d/f will be processed.

This works because Ant properties are not mutable - well, not normally anyway -
so the command-line definition of foo prevents the one within the buildfile from being used.

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