Ant script passing arguments to Batch file - ant

I have an Ant script where I need to call a batch script as follows:
<exec dir="${basedir}\work_internal\${platform}" executable="cmd.exe">
<arg line ="/c example.bat 'C:\work_internal\${platform}' 'revn=120 SPECIAL_OBJS='a b''" />
I need to pass the arguments to example.bat, first argument is a directory and second argument is 'revn=120 SPECIAL_OBJS='a b'', with SPECIAL_OBJS='a b' where 'a b' must be in quotes. But when it calls to Bat script, it discards the quotes around 'a b' so in the second argument it is interpreted as revn=120 SPECIAL_OBJS= a b.
How can make it read like revn=120 SPECIAL_OBJS="a b"?

The single quotes don't pair up how you want them to, but you aught to be able to embed single quotes using the " entity - something like:
<arg line=" ... "revn=120 SPECIAL_OBJS='a b'"" />
For me ant -verbose for the above gives the below:
[exec] Executing 'cmd.exe' with arguments:
[exec] '/c'
[exec] 'example.bat'
[exec] 'C:\work_internal\${platform}'
[exec] 'revn=120 SPECIAL_OBJS='a z''
In your posted xml the quote pairs (v--v) are here:
<arg line="/c example.bat
v----------------------------v v----------------------v vv
'C:\work_internal\${platform}' 'revn=120 SPECIAL_OBJS='a b''" />
which doesn't look like what you intend, and the line is broken up incorrectly.
Another way to pass arguments to the batch script is using separate arg value= elements:
<exec dir="." executable="cmd.exe">
<arg value="/c" />
<arg value="example.bat" />
<arg value="C:\work_internal\${platform}" />
<arg value="revn=120 SPECIAL_OBJS='a b'" />
</exec>
rather than passing everything as a single line to the shell. That sidesteps the (shell) tokenization logic that is breaking the line up differently to how you wish.

Related

Apache Ant property value with interperter

Why can't I set property like this in Apache Ant?
<property name="checker" value="php ${basedir}/vendor/code-checker/src/cc.php" />
And then exec it like this
<exec executable="${checker}">
<arg value="-d" />
<arg path="${basedir}/src" />
</exec>
Instead I have to specify whole path with that scripts' interpreter each time I want to use that checker
<exec executable="php">
<arg value="vendor/code-checker/src/cc.php" />
<arg value="-d" />
<arg path="${basedir}/src" />
</exec>
You can either use executable for the executable you want to run (php in this case) and arg for its arguments, or you can specify the command and all its arguments in a single space-separated attribute
<exec command="${checker} -d ${basedir}/src"/>
You can't mix and match the two. And note that the command form will not work if the basedir contains spaces. If there's any chance of an individual argument containing spaces then there's no choice, you must use the executable and arg form.

exec executable sed

Want to replace value in a txt file on macos using ant.
I used the following code, but its giving error: "The type doesn't support nested text data (" ")".
<exec executable="sed">
<arg value="s/old/new/g" />
<arg value="$MY_FILE" />
</exec>.
How to replace a value of an variable, I used replace for a file on windows, it works.
Maybe it's a bit late, but I just found this question. Hopefully, my solution helps at least other people.
There is one parameter missing. sed on the commandline needs either -e "your expression" of -f "/path/to/your scriptfile". In case of ant usage, you want to change the file directly ("internally" in sed's speech), instead of different input / output files. So you need to add -i (or easier, change the -e to -ie).
So, the correct invocation will be:
<exec executable="sed">
<arg value="-ie" />
<arg value="s/old/new/g" />
<arg value="$MY_FILE" />
</exec>
Maybe this can help
ant-contrib PropertyRegex
Performs regular expression operations on an input string, and sets the results to a property
http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html
In my case I do not need to change my source file, and i need to change some paths into my log4j.properties. So here is my solution
<exec executable="sed">
<arg value="s-/path/to/change/in/log4j_file-/new/path-g"/>
<arg line="./src/log4j.properties"/>
<redirector output="./output/path/conf/log4j.properties" ></redirector>
</exec>
It is like this command:
sed 's-/path/to/change/in/log4j_file-/new/path/log-g' ./src/log4j.properties > ./output/path/conf/log4j.properties

Use ant,How to open a url with multiple parameters

I am using:
<exec executable="cmd">
<arg line="${argLine}" />
</exec>
when argLine
<property name="argLine" value="http://127.0.0.1:/product/findSameStyleSku.json?skuId=305&style=120662"/>
with 2 params ,and i use & escape & symbols
but only open http://127.0.0.1/product/findSameStyleSku.json?skuId=305
the style param lost
In short,i want run
<target name="111test">
<exec executable="cmd" output="e:/test.txt">
<arg line="/c start '' 'http://127.0.0.1/product/findSameStyleSku.json?skuId=305&%3bstyle=120662'" />
</exec>
</target>
2012-05-23 update
Yes,Windows system
I change code to
<target name="111test">
<exec executable="cmd" output="e:/test.txt">
<arg value="/c" />
<arg value="start" />
<arg value="" />
<arg value="http://127.0.0.1/product/findSameStyleSku.json?skuId=305&%3bstyle=120662" />
</exec>
</target>
run ant
only open
http://127.0.0.1/product/findSameStyleSku.json?skuId=305
I change "&%3b" to "&"
also only open
http://127.0.0.1/product/findSameStyleSku.json?skuId=305
But in cmd, i use
start "" "http://127.0.0.1/product/findSameStyleSku.json?skuId=305&style=120662"
can open http://127.0.0.1/product/findSameStyleSku.json?skuId=305&style=120662
Not exactly understanding what it's trying to open. Is it literally trying to open a URL with ***?skuId=305, or are you trying to say it's trying to open the URL you gave it up to, but not including the semicolon?
If you are saying that it is leaving off the last part of your URL, you should understand that semicolons cannot be part of the actual URL you're sending. They're reserved and must be specially encoded.
Make sure that semicolon is even suppose to be there. When you do a GET request, you have the basic URL, and then after that URL a question mark. After that, you have a series of parameters you're passing to the URL with each parameter separated by ampersands. In your case, it looks like you want to send a request to URL:
http://127.0.0.1:/product/findSameStyleSku.json
With the following two parameters:
skuId = 305
style = 120662
So, it looks like the semicolon is bogus. Otherwise, you're passing the parameter ;style and not style. And, that doesn't seem it would be correct.
If you've determined that the semicolon is really suppose to be there, try replacing the semicolon in the URL with %3b:
<property name="argLine" value="http://127.0.0.1:/product/findSameStyleSku.json?skuId=305&%3bstyle=120662"/>
Response
,i'm sorry ,it does't Work correctly,I updated my question – feeling
Unfortunately, since this is on a Windows machine, and is something running locally on your machine, I can't run a test myself.
However, according to the first part of your post, you are attempting to run the following command:
C> cmd http://127.0.0.1:/product/findSameStyleSku.json?skuId=305&style=120662
Later in your post, you say you want to run:
C> cmd /c start '' 'http://127.0.0.1/product/findSameStyleSku.json?skuId=305&%3bstyle=120662'
The first certainly won't work from the command line. Does the second one?
I've done a few tests on my current computer using this:
<property name="argLine"
value="http://etzahaim.org/index.php?option=com_content&task=blogcategory&id=15&Itemid=34"/>
<exec executable="curl">
<arg value="${argLine}"/>
<arg value="--include"/>
<arg value="--output"/>
<arg value="curl.out"/>
</exec>
This is the curl command which fetches the URL. This URL is also uses a GET method, so it also has a question mark and some asterisks in the URL. This works for me.
Try switching from <arg line='start' '' 'http://...> to using <arg line> and see if that helps:
<target name="111test">
<exec executable="cmd" output="e:/test.txt">
<arg value="/c"/>
<arg value="start"/>
<arg value=""/>
<arg value="${argline}" />
</exec>

How to get %ERRORLEVEL% from batch file in Ant

I want to know if its possible to get the return value from batch file in Ant build xml.
My batch file returns %ERRORLEVEL% value (batch file returns 2 in my case). I want to know if it's possible to capture this and mark as error in Ant. Below is the code snippet I use:
<exec executable = "cmd">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
Currently after the batch file call, build is reported as success always. It looks like Ant is not processing the %ERRORLEVEL% or I am not sure. How I can make Ant process the %ERRORLEVEL%?
Use the resultproperty and failonerror. By default, the errocode is ignored.
<property name="Batcherrcode" value="0"/>
<exec executable = "cmd" failonerror="true" resultproperty="Batcherrcode">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
<echo message="Error Code:=${Batcherrorcode}" />

EXEC task with '|' in the arguments

I'm trying to exec a VS build using incredibuild within my ANT script, but For some reason the exec task fails with the following error:
'Win32' is not recognized as an internal or external command
when I use the following code:
<arg line='buildconsole solution.sln /rebuild /cfg="Release|Win32"' />
I think the ant script may be treating the '|' as a delimter or something...
Any ideas how I could get this to work?
I've also tried the following, but nothing gets me closer:
<arg line='buildconsole solution.sln /rebuild /cfg="Release|Win32"' />
<arg value="buildconsole solution.sln /rebuild /cfg="Release|Win32"" />
<arg value="buildconsole solution.sln /rebuild /cfg="Release|Win32"" />
You need to escape the pipe symbol by preceding it with ^. So:
<arg line='buildconsole solution.sln /rebuild /cfg="Release^|Win32"' />
EDIT:
Are you sure the caret doesn't work? It seems to in this sample ant file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="build" basedir=".">
<target name="build">
<exec executable="cmd">
<arg line="/k echo cfg="Release^|Win32""/>
</exec>
</target>
</project>
Hmm... I just tried it again and it worked, but only after I changed to
<arg value="buildconsole solution.sln /rebuild /cfg=Release^|Win32" />
so I guess the quotes around Release^|Win32 wasn't necessary if I use value.
Thanks a bunch!
I think the problem is that the Windows command prompt sees the | and treats it as a "pipe" operator. Perhaps escape the pipe by using:
<arg line='buildconsole solution.sln /rebuild /cfg="Release\|Win32"' />

Resources