【问题标题】:Ant rule/task that can forward a variable number of cmd line arguments to a task可以将可变数量的命令行参数转发给任务的 Ant 规则/任务
【发布时间】:2011-11-23 03:23:41
【问题描述】:

我想要一个包含命令行传递参数的 ant 任务。命令行参数的数量可能不同。 具体来说,对于 ant 中的 任务。

我想在命令行上执行此操作:

$ ant run foo bar ...

理想情况下,“foo”和“bar”以及其他参数“...”将作为尾随参数传递给在 任务中创建的 java 实例。
java会看到:

    $ java -classpath ./output Foobar foo bar ...

换句话说,我希望相同的 ant 任务执行以下操作:

$ ant run foo 
# executes "java -classpath ./output Foobar foo"

$ ant run foo bar
# executes "java -classpath ./output Foobar foo bar"

$ ant run foo bar baz
# executes "java -classpath ./output Foobar foo bar baz"



我想这可能看起来像:

<project name="Foobar" basedir=".">
    <property name="build" location="output"/>
    <target name="run" >
        <java failonerror="true" classname="Foobar" fork="true">
            <classpath>
                <dirset dir="${build}" />
            </classpath>
            <arg line="$@"/>
        </java>
    </target>
</project>

注意这条线

            <arg line="$@"/>

我想像上面这样的东西会将所有剩余的参数传递给 java 实例。 (这个问题的目的是找到那个特定的蚂蚁机制)。




我为这个require preconfigured ant variables 看到的方法。也就是说,

 $ ant run -DARG1="foo" -DARG2="bar" ...

但该方法排除了可变长度参数列表。

有没有人知道可以将可变数量的参数转发给 ant 任务的方法(最好不需要编写一组复杂的 ant 规则)?

【问题讨论】:

    标签: ant


    【解决方案1】:

    这并不容易,因为 Ant 的 command line 看起来像这样:

    ant [options] [target [target2 [target3] ...]]
    

    这意味着您的变量 arg 列表 foo bar baz 项目将被视为目标,从而导致运行类似名称的 Ant 目标,或者如果它们不存在则 Ant 抛出错误。

    一种选择是使用-Doption=value 语法通过包装脚本传递变量参数列表。也许:

    ant -classpath ./output -Dmy_args=\"$@\" Foobar
    

    在 shell 脚本中,然后在 java 任务中使用传递的 arg:

    <arg line="${my_args}"/>
    

    另一种选择是编写一个 Java main() 类,该类将接受您希望的参数,然后为您调用 Ant。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      相关资源
      最近更新 更多