Java 使用 Ant 运行带有命令行参数的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3730880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:01:54  来源:igfitidea点击:

Use Ant for running program with command line arguments

javaant

提问by Victoria Seniuk

My program getting command line arguments. How can I pass it when I use Ant?

我的程序获取命令行参数。我在使用 Ant 时如何通过它?

采纳答案by emory

Extending Richard Cook's answer.

扩展Richard Cook的回答。

Here's the anttask to run any program (including, but not limited to Java programs):

这是ant运行任何程序(包括但不限于 Java 程序)的任务:

<target name="run">
   <exec executable="name-of-executable">
      <arg value="${arg0}"/>
      <arg value="${arg1}"/>
   </exec>
</target>

Here's the task to run a Java program from a .jarfile:

下面是从.jar文件运行 Java 程序的任务:

<target name="run-java">
   <java jar="path for jar">
      <arg value="${arg0}"/>
      <arg value="${arg1}"/>
   </java>
</target>

You can invoke either from the command line like this:

您可以像这样从命令行调用:

ant -Darg0=Hello -Darg1=World run

Make sure to use the -Dargsyntax; if you ran this:

确保使用-Darg语法;如果你运行这个:

ant run arg0 arg1

then antwould try to run targets arg0and arg1.

然后ant将尝试运行目标arg0arg1.

回答by Richard Cook

Can you be a bit more specific about what you're trying to do and how you're trying to do it?

您能否更具体地说明您正在尝试做什么以及如何尝试?

If you're attempting to invoke the program using the <exec>task you might do the following:

如果您尝试使用该<exec>任务调用程序,您可以执行以下操作:

<exec executable="name-of-executable">
  <arg value="arg0"/>
  <arg value="arg1"/>
</exec>

回答by Mark O'Connor

The only effective mechanism for passing parameters into a build is to use Java properties:

将参数传递到构建中的唯一有效机制是使用 Java 属性:

ant -Done=1 -Dtwo=2

The following example demonstrates how you can check and ensure the expected parameters have been passed into the script

以下示例演示了如何检查并确保已将预期参数传递到脚本中

<project name="check" default="build">

    <condition property="params.set">
        <and>
            <isset property="one"/>
            <isset property="two"/>
        </and>
    </condition>

    <target name="check">
        <fail unless="params.set">
        Must specify the parameters: one, two
        </fail>
    </target>

    <target name="build" depends="check">
        <echo>
        one = ${one}
        two = ${two}
        </echo>
    </target>

</project>

回答by ofavre

If you do not want to handle separate properties for each possible argument, I suggest you'd use:

如果您不想为每个可能的参数处理单独的属性,我建议您使用:

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

You can check if the property is not set using a specific target with an unlessattribute and inside do:

您可以检查是否未使用具有unless属性的特定目标设置该属性,并在内部执行:

<input message="Type the desired command line arguments:" addProperty="args"/>

Putting it all together gives:

把它们放在一起给出:

<target name="run" depends="compile, input-runargs" description="run the project">
  <!-- You can use exec here, depending on your needs -->
  <java classname="Main">
    <arg line="${args}"/>
  </java>
</target>
<target name="input-runargs" unless="args" description="prompts for command line arguments if necessary">
  <input addProperty="args" message="Type the desired command line arguments:"/>
</target>

You can use it as follows:

您可以按如下方式使用它:

ant
ant run
ant run -Dargs='--help'

The first two commands will prompt for the command-line arguments, whereas the latter won't.

前两个命令将提示输入命令行参数,而后者则不会。

回答by Hugh Perkins

What I did in the end is make a batch file to extract the CLASSPATH from the ant file, then run java directly using this:

我最后做的是制作一个批处理文件从ant文件中提取CLASSPATH,然后使用这个直接运行java:

In my build.xml:

在我的 build.xml 中:

<target name="printclasspath">
    <pathconvert property="classpathProp" refid="project.class.path"/>
    <echo>${classpathProp}</echo>
</target>

In another script called 'run.sh':

在另一个名为“run.sh”的脚本中:

export CLASSPATH=$(ant -q printclasspath | grep echo | cut -d \  -f 7):build
java "$@"

It's no longer cross-platform, but at least it's relatively easy to use, and one could provide a .bat file that does the same as the run.sh. It's a very short batch script. It's not like migrating the entire build to platform-specific batch files.

它不再是跨平台的,但至少它相对易于使用,并且可以提供一个与 run.sh 相同的 .bat 文件。这是一个非常短的批处理脚本。这不像将整个构建迁移到特定于平台的批处理文件。

I think it's a shame there's not some option in ant whereby you could do something like:

我认为很遗憾在 ant 中没有一些选项可以让您执行以下操作:

ant -- arg1 arg2 arg3

mpirun uses this type of syntax; ssh also can use this syntax I think.

mpirun 使用这种类型的语法;我认为 ssh 也可以使用这种语法。