Running a .cmd file from Ant

2020-03-19 03:49发布

问题:

Is it possible to run a command (.cmd file) from Ant? Would I need to write Java code for this?

回答1:

<exec executable="cmd" os="Windows XP">
  <arg value="/C"/>
  <arg value="command to run"/>
</exec>


回答2:

You can do this by using the exec task. From the ant exec documentation:

Note that .bat files cannot in general by executed directly. One normally needs to execute the command shell executable cmd using the /c switch.

So you would need to do something like:

<exec executable="cmd">
  <arg value="/c"/>
  <arg value="batchfile.cmd"/>
</exec>

Note that by doing this you have created a dependency of running your ant script in windows.



回答3:

Adding to eradicus answer, you can also execute .bat, .cmd, ... from any directory with argument on your Window machine by

<target name="test">        
    <exec executable="cmd" dir="C:/Users/mydir/">
        <arg value="/C" />
        <arg value="myjob.bat arg1 arg2" />                         
    </exec>     
</target>


标签: ant