I need to exec the following command from ant, but I can't figure out how to escape the double-quotes:
tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"
Ant uses XML, so you can use the normal XML entities like ":
tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"
Ant script is xml. So in xml, here is the rule.
For > use >
For < use <
For “ use "
For & use &
For ‘ use '
Notice! ";"
Reference:
http://www.jguru.com/faq/view.jsp?EID=721755
I don't believe you really do if you use <arg value> and not <arg line>:
tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"
<exec executable="tasklist">
<arg value="/FI"/>
<arg value="IMAGENAME eq java.exe"/>
<arg value="/FI"/>
<arg value="MEMUSAGE gt 50000"/>
</exec>
Despite the spaces, the <arg value> will send it as a single parameter to the command. Unless the command itself requires quotes, this should work.
But doesn't work if you need to use the find DOS command in a /CMD exec task:
<target name="install" depends="install2">
<exec executable="cmd.exe" outputproperty="result.process">
<arg line='/c tasklist | find "httpd"'/>
</exec>
<echo message="RESULT: ${result.process}" />
</target>
gives,
install:
[exec] Current OS is Windows 7
[exec] Output redirected to property: result.process
[exec] Executing 'cmd.exe' with arguments:
[exec] '/c'
[exec] 'tasklist'
[exec] '|'
[exec] 'find'
[exec] 'httpd'
[exec]
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.
[exec] Result: 2
[echo] RESULT: FIND : format incorrect de paramètre
Its like if ANT deletes the double quotes around the parameter when it is passed to the CMD interpereter. The help for the find DOS function says you need to use double quotes for the text you're looking for.
Here is an example http://ant.apache.org/faq.html#shell-redirect-2. Simply use single quotes as xml parameter separator. This way you could freely use double quotes inside the arguments.
Related
The following ANT exec tag does not behave as expected.
<exec executable="c:\scratch\test.cmd">
<arg value="A,B,C"/>
</exec>
When executed, I would expect this to call text.cmd with 1 argument. However, the arg is being expanded to three separate arguments.
As per the Manual, value is supposed to pass the contents as a single argument, however, it is passed as three (one for each component of the string separated by a comma).
I tried replacing the command "," with a semicolon (;) but this does not work either. It appears as if arg's value attribute parses the supplied string as if it were a path, which it is not.
Anyone know how to the "A,B,C" to pass as one argument?
For the sake of completeness, my test.cmd file is this:
#echo off
echo Arg1: %1
echo Arg2: %2
echo Arg3: %3
echo Arg4: %4
echo Arg5: %5
echo Arg6: %6
echo Arg7: %7
echo Arg8: %8
echo Arg9: %9
and the output of the ant build is:
[exec] Arg1: A
[exec] Arg2: B
[exec] Arg3: C
[exec] Arg4:
[exec] Arg5:
[exec] Arg6:
[exec] Arg7:
[exec] Arg8:
[exec] Arg9:
Issue has been resolved. I was so focused on the issue being in ant, that I didn't take the time to test how DOS like command lines interpret the command-line arguments.
from Window command line, I ran test.cmd a,c,b and see that the command argument was split, therefore, the issue is not related to ant. so now I just need to figure out how to force ANT to quote the arguments.
Try:
<exec executable="c:\scratch\test.cmd">
<arg line="A,B,C"/>
</exec>
See ant manual for description of how arguments work.
I want an ant task that includes command line passed arguments. The command line arguments can vary in number.
Specifically, for the <java> task within ant.
I would like to do this on the command line:
$ ant run foo bar ...
Ideally, "foo" and "bar" and other arguments "...", would be passed as trailing arguments to the java instance created in the <java> task.
java would see:
$ java -classpath ./output Foobar foo bar ...
In other words, I would like the same ant <java> task do the following:
$ ant run foo
# executes "java -classpath ./output Foobar foo"
$ ant run foo bar
# executes "java -classpath ./output Foobar foo bar"
$ ant run foo bar baz
# executes "java -classpath ./output Foobar foo bar baz"
I imagined this might look something like :
<project name="Foobar" basedir=".">
<property name="build" location="output"/>
<target name="run" >
<java failonerror="true" classname="Foobar" fork="true">
<classpath>
<dirset dir="${build}" />
</classpath>
<arg line="$#"/>
</java>
</target>
</project>
Notice the line
<arg line="$#"/>
I imagined something like the above would pass all remaining arguments to the java instance. (The purpose of this Question is to find that particular ant mechanism).
The methods I have seen for this require preconfigured ant variables. That is,
$ ant run -DARG1="foo" -DARG2="bar" ...
But that method precludes a variable length argument list.
Does anyone know a method for a variable number of argument that could be forwarded to an ant <java> task (preferably doesn't require writing a complex set of ant rules)?
That's not easy because Ant's command line looks like this:
ant [options] [target [target2 [target3] ...]]
That means your variable arg list foo bar baz items would be treated as targets, leading to similarly named Ant targets being run, or Ant throwing errors if they don't exist.
One option is to use the -Doption=value syntax to pass the variable argument list by means of a wrapper script. Perhaps:
ant -classpath ./output -Dmy_args=\"$#\" Foobar
in a shell script, then make use of the passed arg in the java task:
<arg line="${my_args}"/>
Another option would be to write a Java main() class that will accept the arguments as you want them to be, and then call Ant for you.
I'm trying to execute iisvdir from an ant script to clean and create a virtual directory before I compile my .net app in Visual Studio. I am running into a couple of strange errors one one build server, but another is running the script without any problem.
<exec dir="${SYSTEM32}" executable="cscript" failonerror="true">
<arg line='iisvdir.vbs /create "Default Web Site" ${RS_VIRTUAL_DIR} "${env.WORKSPACE}"'/>
</exec>
Results in:
[exec] Microsoft (R) Windows Script Host Version 5.6
[exec] Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
[exec]
[exec] Input Error: Can not find script file "c:\windows\system32\iisvdir.vbs".
And then
<exec dir="${SYSTEM32}" executable="cmd" failonerror="true">
<arg line='cscript iisvdir.vbs /create "Default Web Site" ${RS_VIRTUAL_DIR} "${env.WORKSPACE}"'/>
</exec>
Results in
[exec] 'reate' is not recognized as an internal or external command,
[exec] operable program or batch file.
Can someone help me figure out what might be wrong?
Is iisvdir.vbs where you say it is?
To get CMD.EXE to run a command, you need to use the /C switch.
For example:
cmd.exe echo Hello
...ignores the parameters and runs another interactive command prompt as a subshell.
cmd.exe /c echo Hello
...runs the "echo Hello" statement and returns immediately. Note: You can use /K if you want cmd.exe to continue running interactively after running the statement (not usually a good idea in a build script).
Your command:
cmd.exe cscript iisvdir.vbs /create etc.
...is getting parsed as if you'd really said:
cmd.exe /c reat etc.
This is because cmd.exe has (as with most MS command line tools) freaky command line parsing.
Update: Is this a 64-bit OS? If Ant is a 32-bit task, then it'll actually (silently) be looking in C:\Windows\SysWOW64 for cscript.exe and iisvdir.vbs. Are they there? If not, you should use C:\Windows\SysNative. In a 32-bit task, this is aliased to the real C:\Windows\System32 directory.
I don't know if it's the cause of your problems but I notice that you are using a single quote (') for <arg line='. All the examples I've seen use a double quote (") I know you are enclosing items with spaces in double quotes so it may be necessary to escape them out? Perhaps moving the code into a batch file which you can test before running via Ant?
Not sure if this will help but could point you in the right direction.
bash shell:
./mimic_cmd "startDaemon()"
Corresponding Ant code:
<exec failonerror="true" executable="/bin/mimic_cmd">
<arg value='"startDaemon()"' />
</exec>
Does the Ant code exactly represent the above command at the bash shell? Based on the debug info, it looks like it:
[exec] Executing '/bin/mimic_cmd' with arguments:
[exec] '"startDaemon()"'
[exec]
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.
Execute:Java13CommandLauncher: Executing '/bin/mimic_cmd' with arguments:
'"startDaemon()"'
The ' characters around the executable and arguments are not part of the command.
However, the Ant code returns and exit code of 1 while the Bash shell command returns 0.
Toggling vmlauncher doesn't help, and paths are all correct.
The same Ant code works on windows with the resulting debug output:
[exec] Executing 'C:\bin\mimic_cmd' with arguments:
[exec] '"startDaemon()"'
[exec]
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.
Execute:Java13CommandLauncher: Executing 'C:\bin\mimic_cmd' with arguments:
'"startDaemon()"'
The ' characters around the executable and arguments are not part of the command.
Can you tell us what mimic_cmd is? (Is it an ELF executable, is it a script -- and if so, what is its contents?)
You don't need nor want the double-quotes inside your ANT XML attributes (incidentally, for it to be well-formed XML you should have written them as " not ", but that changes nothing with respect to this discussion) unless your executable expects them. The corresponding ANT code for either of the following (100% equivalent) shell command lines:
./mimic_cmd "startDaemon()"
./mimic_cmd 'startDaemon()'
./mimic_cmd startDaemon\(\)
./mimic_cmd startDaemon"()"
./mimic_cmd startDaemon'()'
...actually is:
<exec failonerror="true" executable="/bin/mimic_cmd">
<arg value="startDaemon()" />
</exec>
...or, for illustrative purposes:
<!-- spawn a shell with your original command line -->
<exec failonerror="true" executable="/bin/sh">
<arg value="-c" />
<arg value="/bin/mimic_cmd "startDaemon()"" />
</exec>
Why that is so is longwinded to explain; suffices to say that, in your specific case, the only time when you'd have to use double quotes would be when ultimately issuing the command via a *nix shell (either interactively or as part of another script or programatically via the execing of sh -c), and only in order for that shell not to think that the round parens () have special meaning. By the time the shell would in turn spawn mimic_cmd it would have already stripped the double quotes (and substituted backslash-escaped sequences etc. -- see how a *nix shell parses its command line) ANT does not run your command via the shell but rather executes it directly, so in this case mimic_cmd finds itself with a bunch of double quotes on its hand which it apparently doesn't know how to handle.
You essentially have to think of it as replacing all forms of shell quoting and escaping with XML escaping and breaing down into <arg/> tags.
Windows' CMD.EXE is special in the sense that, unline *nix shells, it does minimal parsing (and generally does not care about double quotes in program arguments), leaving it up to the program to figure out what you meant by quoting. (This is actually a hard limitation of Windows' CreateProcess which does not have the notion of argv[], leaving it up to each program to intepret lpCommandLine in whichever way it sees fit; some will get rid of the quotes for you, but that behaviour is extremely inconsistent, e.g. issue echo "bla" on the CMD.EXE prompt to see what CMD.EXE's builtins think about quoting.) Again, in your case the round parens () have no meaning for CMD.EXE so you don't need them even when typing the command at a command prompt. As for ANT, on Windows as on *nix platforms, it spwans mimic_cmd via CreateProcess not CMD.EXE so you don't really want to quote anything.
The Ant exec task has an output property which can be used to tell Ant where the output goes. I've used it to redirect the output to a file. The thing is, if I don't do something with the output, the stuff that Ant prints isn't that much of a help - it's not complete.
Is there someway of setting the output property to System.out?
When executing a batch file with ant's apply or exec tasks on Windows, I found there are special cases where some of the stdout and stderr is not captured by ant. (For example: if you call a batch file that in turn calls other commands (like node.exe), then the stdout and stderror from the child node.exe process is lost.)
I spent a long time trying to debug this! It seems that the batch file's stdout and stderr is captured, however commands called by the batch file are somehow not seen by ant. (perhaps because they are separate child processes). Using the output and error attributes as suggested above doesn't help because only some of the stdout and/or stderr is captured.
The solution I came up with (a hack) is to add these arguments at the end of the command:
<!--Next arg: forces node's stderror and stdout to a temporary file-->
<arg line=" > _tempfile.out 2<&1"/>
<!--Next arg: If command exits with an error, then output the temporary file to stdout, -->
<!--delete the temporary file and finally exit with error level 1 so that -->
<!--the apply task can catch the error if #failonerror="true" -->
<arg line=" || (type _tempfile.out & del _tempfile.out & exit /b 1)"/>
<!--Next arg: Otherwise, just type the temporary file and delete it-->
<arg line=" & type _tempfile.out & del _tempfile.out &"/>
Because this hack only applies to windows, remember to add #osfamily="windows" to the apply or exec task. And create similar task(s) for `#osfamily="unix", etc but without these extra arguments.
The output of exec does go to standard out unless you specify the output attribute.
If you want to output to System.out, then simply do not specify the "output" attribute. If you would like to redirect to a file AND print it to System.out, you can use the tee command, which will redirect output to a given file and also echo it to standard out... I do not know if Windows supports "tee" or an equivalent.
Maybe you want to look at the error, logError, and errorproperty attributes of the exec task too. These deal with the handling of the standard error stream from the exec'd process. There may be useful information there that is going awol for some reason - which might account for the incompleteness you see.
But, if the exec'd process decides to close stdout or stderr and send them elsewhere - there's little you can do.
I have faced similar problem: the output of command execution was suppressed. Perhaps that is the side effect when running cmd under WinXP (I an using maven-antrun-plugin). Anyway setting output="con" worked out perfectly:
<configuration>
<target>
<exec executable="cmd" output="con">
<arg value="/c" />
<arg value="..." />
</exec>
</target>
</configuration>
Working with Ant and Gruntjs:
For anyone trying to get this to work using Gruntjs. I was able to get it working by doing the following (in combination with darcyparker's answer).
In my Ant Build File:
<target description="run grunt js tasks" name="grunt">
<exec dir="/path/to/grunt" executable="cmd" failonerror="true">
<arg value="/c"/>
<arg value="jshint.bat"/> // I broke each task into it's own exec
<arg line=" > jshint.log 2<&1"/>
<arg line=" || (type jshint.log & del jshint.log & exit /b 1)"/>
<arg line=" & type jshint.log & del jshint.log &"/>
</exec>
<exec dir="/path/to/grunt" executable="cmd" failonerror="true">
// another grunt task (IE: uglify, cssmin, ect..)
</exec>
</target>
jshint.bat
#echo off
pushd "C:\path\to\grunt\"
#ECHO _____________________________________________
#ECHO GRUNT JSHINT
#ECHO _____________________________________________
grunt jshint --stack >>jshint.log
NOTE: Path to grunt would be where your Gruntfile.js is located. Also note, I had to initially create the log file (to get it to work with darcyparker's answer) which would output the stack trace from that particular task. This would then give me the grunt task stack output from wherever I call my ant target.
Finally note that pushd "C:\path\to\grunt\" won't be necissary if your bat files are in the same directory as your Gruntfile.js.
I was experiencing this same kind of issue trying to get the build process to fail in Ant after Karma tests intentionally failed, and executing them with "grunt test".
Just added /c before "grunt test", and it worked like a charm
<target name="unittest">
<echo>*** KARMA UNIT TESTING ***</echo>
<exec dir="api_ui" executable="cmd" osfamily="windows" logError="yes" failonerror="true">
<arg value="/c grunt test"/>
</exec>
</target>