Calling system commands or external programs from Ant

zhaozj2021-02-16  56

Calling system commands or external programs from Ant

Recently, Ant has been used, and there are still many functions that Ant does not provide corresponding Task support, and the operating system provides the corresponding system command. The Ant manual said, use

You can call the system command. It is actually to find that the trap is a lot, and you will fall if you accidentally. Below to list the files in the current directory as an example. Note that I am using Ant 1.5.4. For the Windows platform, the command is: DIR / O: -D For UNIX (including Linux, the same) platform, the command is ls -ltr

Trap 1: Who is called?

This has already been mentioned in the ANT document. Under Windows, DIR cannot be called directly, Del (because this program!), Etc. must be called by cmd.exe:

For UNIX, you can call LS directly:

You can also specify by shell call

I think the latter is better, otherwise it is not possible to guarantee whether the system command is called because as long as the path environment variable indicated the same name under the path indicated by the PATH environment variable, it may be called. With shell call, Windows must be added / c, and UNIX is plus -c.

Trap 2: Arg Line, Arg Value also has quotation marks

If we expand the example under the Windows platform to meet the requirements of the modification time:

success. Change UNIX:

Sorry, failure: java.lang.ioException. The reason is because

The string separated by each space is transmitted to the calling program, but for "/ bin / sh", "LS -LTR" should be a parameter rather than two. There are two solutions: plus quotes:

Use

:

Trap 3: Block (Block)

Whether it is Windows or UNIX, you must wait until the end of this operation will continue to continue. If you want to call a program, you must use other work.

:

Need to pay attention to:

DIR indicates the path execution of the program. The search program also affects the platform Failonerror that the OS indicated execution is false, that is, the command executes but fail, build is still continuing

转载请注明原文地址:https://www.9cbs.com/read-25269.html

New Post(0)