【问题标题】:How do I specify a file to pass to a java process run by an Ant task?如何指定要传递给 Ant 任务运行的 java 进程的文件?
【发布时间】:2010-12-08 18:37:25
【问题描述】:

我有一个 Ant 任务来调用一个 java 进程,该进程在命令行上获取一个文件。我可以将文件直接传递给 java 程序,但我不知道如何让 Ant 在命令行上获取文件。

这是我得到的:

<target name="FileProcessor" description="Process a specified file">
    <run-standalone name="CheckClearer" main-class="com.blah.FileProcessor">
        <args>
            <arg value="${file}"/>
        </args>
    </run-standalone>
</target>

当我运行它时,我得到了

Exception in thread "main" java.io.FileNotFoundException: ${file} (No such file or directory)

(我到处寻找答案,因为我确定我只是遗漏了一些简单的东西,但没有找到任何东西。如果那里有 SO 答案,请指点我。谢谢)

【问题讨论】:

  • 您是否尝试在run-standalone 之前添加&lt;echo&gt;file: ${file}&lt;/echo&gt;

标签: java command-line ant


【解决方案1】:

我从未见过&lt;run-standalone&gt; 任务,但您可以只使用&lt;java&gt; 来启动Java 类并嵌套&lt;arg&gt; 来指定类的参数。

例子:

<target name="FileProcessor" description="Process a specified file">
    <java classname="com.blah.FileProcessor">
        <classpath>...</classpath>
        <arg file="${file}"/>
    </java>
</target>

当然,首先要确保属性${file}中的值确实存在。

【讨论】:

    【解决方案2】:

    尝试这样调用ant:

    ant FileProcessor -Dfile=<your path here> 
    

    【讨论】:

    • 只有系统属性才能从 build.xml 中访问 ant 命令行中指定的值。
    【解决方案3】:

    看起来file 属性未设置。

    &lt;run-standalone&gt;之前定义属性file

    <property name="file" location="/path/to/file"/>
    <run-standalone>
    ...
    </run-standalone>
    

    看起来您正在使用用户定义的宏或自定义任务run-standalone。因为它不是 standad Ant 的一部分,所以很难说这个特定的 API 期望什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多