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

时间:2022-09-06 14:08:58

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

我的程序获得命令行参数。使用Ant时如何传递它?

5 个解决方案

#1


70  

Extending Richard Cook's answer.

延长理查德·库克的答案。

Here's the ant task 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 .jar file:

下面是从.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 -Darg syntax; if you ran this:

确保使用-Darg语法;如果你跑:

ant run arg0 arg1

then ant would try to run targets arg0 and arg1.

然后ant尝试运行arg0和arg1目标。

#2


26  

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 unless attribute and inside do:

您可以检查属性是否没有使用具有除非属性的特定目标进行设置,并在内部进行:

<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.

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

#3


10  

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>

#4


4  

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 executable="name-of-executable">
  <arg value="arg0"/>
  <arg value="arg1"/>
</exec>

#5


0  

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文件中提取类路径,然后使用以下方法直接运行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.

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

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还可以使用这种语法。

#1


70  

Extending Richard Cook's answer.

延长理查德·库克的答案。

Here's the ant task 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 .jar file:

下面是从.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 -Darg syntax; if you ran this:

确保使用-Darg语法;如果你跑:

ant run arg0 arg1

then ant would try to run targets arg0 and arg1.

然后ant尝试运行arg0和arg1目标。

#2


26  

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 unless attribute and inside do:

您可以检查属性是否没有使用具有除非属性的特定目标进行设置,并在内部进行:

<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.

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

#3


10  

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>

#4


4  

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 executable="name-of-executable">
  <arg value="arg0"/>
  <arg value="arg1"/>
</exec>

#5


0  

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文件中提取类路径,然后使用以下方法直接运行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.

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

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还可以使用这种语法。