Apache Ant property value with interperter - ant

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.

Related

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>

ant send yes or no

I am just stuck at thing scenario,
I have a batch file which upon running will ask for confirmation like " press y/n ". Now i am to automate that batch file using ant. so, my code looks something like this
<exec executable="cmd.exe" dir="${base.dir}" >
<arg line="/c run.bat" />
</exec>
but I have no idea how to pass the keyboard value 'y' to it in run time
please help me out
Use the input task?
Use combination of input task and inputstring parameter of exec task.
<input
message="All data is going to be deleted from DB continue (y/n)?"
validargs="y,n"
addproperty="do.delete"
/>
<exec
executable="cmd.exe"
dir="${base.dir}"
inputstring="${do.delete}"
>
<arg line="/c run.bat" />
</exec>
Just provide /y input as you would without ant:
<exec executable="cmd.exe" dir="${base.dir}" >
<arg line="/c run.bat /y "/>
</exec>
Another way is to use inputstring task.
E.g.:
<exec executable= "run.bat" failonerror="true" inputstring="Y">
</exec>
However this proved to be unstable in some scenarios.

Trouble passing argument to Ant exec task

I'm using Ant 1.8. I want to pass a property that I define in my script to an exec command. Although I can see the property has a value in my echo statements, when I pass it to the script and output its value in the script, its value prints out as "${myco.test.root}", without being converted. What is the correct way to pass the property's value to the script? Below is the relevant code from my build.xml file …
<target name="checkout-selenium-tests" depends="set-critical-path-test-suite,set-default-test-suite,check-local-folders-exist">
<echo message=" test root ${myco.test.root}" />
<stcheckout servername="${st.servername}"
serverport="${st.serverport}"
projectname="${st.myco.project}"
viewname="${st.myco.viewname}"
username="${st.username}"
password="${st.password}"
rootstarteamfolder="${myco.starteam.test.root}"
rootlocalfolder="${myco.test.root}"
forced="true"
deleteuncontrolled="true"
/>
<delete file="${myco.testsuite.file}" />
<echo message="test root ${myco.test.root}" />
<exec failonerror="true" executable="perl" dir="${scripts.dir}">
<arg value="generate_test_suite.pl" />
<arg value="My Tests" />
<arg value="${myco.test.root}" />
<arg value="${myco.testsuite.file}" />
</exec>
</target>
Thanks, - Dave
It actually looks good to me. Try running the build.xml with both the verbose and debug options turned on in Ant:
ant -d -v checkout-selenium-tests
That'll help trace down where the error could be coming from.

Running a .cmd file from Ant

Is it possible to run a command (.cmd file) from Ant? Would I need to write Java code for this?
<exec executable="cmd" os="Windows XP">
<arg value="/C"/>
<arg value="command to run"/>
</exec>
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.
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>

How can I get current PID from within Ant?

I have an ant task, and within it I'd like to get the current process id (a la echo $PPID from command line).
I'm running ksh on Solaris, so I thought I could just do this:
<property environment="env" />
<target name="targ">
<echo message="PID is ${env.PPID}" />
<echo message="PID is ${env.$$}" />
</target>
But that didn't work; the variables aren't substituted. Turns out PPID, SECONDS, and certain other env variables don't make it into Ant's representation.
Next I try this:
<target name="targ">
<exec executable="${env.pathtomyfiles}/getpid.sh" />
</target>
getpid.sh looks like this:
echo $$
This gets me the PID of the spawned shell script. Closer, but not really what I need.
I just want my current process ID, so I can make a temporary file with that value in the name. Any thoughts?
You can find PID using java process monitoring tool JPS, then output stream can be filtered and if needed process can be killed. check out this tomcat pid kill script:
<target name="tomcat.kill" depends="tomcat.shutdown">
<exec executable="jps">
<arg value="-l"/>
<redirector outputproperty="process.pid">
<outputfilterchain>
<linecontains>
<contains value="C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</linecontains>
<replacestring from=" C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</outputfilterchain>
</redirector>
</exec>
<exec executable="taskkill" osfamily="winnt">
<arg value="/F"/>
<arg value="/PID"/>
<arg value="${process.pid}"/>
</exec>
<exec executable="kill" osfamily="unix">
<arg value="-9"/>
<arg value="${process.pid}"/>
</exec>
</target>
Why not just use the tempfile Ant task, instead? It does what you really want to do, while hiding all the gory details.
See http://ant.apache.org/manual/Tasks/tempfile.html.
your second method doesn't get ANT's pid. Change the shell script to (I use bash, I don't know if ksh is the same):
echo "$PPID"

Resources