Trouble passing argument to Ant exec task - ant

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.

Related

How to set the path environment variable from ant script

How to set the path environment variable from ant script
Is this for an <exec> task?
You can set environment variables when you run an <exec> task:
<exec executable="${my.command}">
<env key="foo" value="bar"/>
<arg line="some value"/>
</exec>
You can use <property environment="env"/> to expand the path:
<property environment="env"/>
<exec executable="${my.command}">
<env key="PATH" value="${env.PATH}:${my.directory}"/>
</exec>
If this is for some custom task that requires an environment variable, but doesn't allow you to set the environment variable in the task if one isn't set, you can try setting it in:
<property environment="env"/>
<property name="env.foo" value="bar!bar"/>
This might set an environment variable called foo to the value of bar!bar!. I remember something about this, but wasn't able to get it to work.
The other thing you can do is have one ant script execute another and have the first ant script set the environment value. I did this when I had to set ANT_OPT.
In ant, properties are immutable, so David's suggestion above:
<property name="env.foo" value="bar!bar"/>
won't work.
But (with the antcontrib-library) variables are mutable, so this works:
<var name="env.foo" value="bar!bar"/>
NOTE: to use the antcontrib-library download it from here: ANT Contrib - Download
This gets the job done, but seems like a dastardly trick.
So to your specific question, try:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${basedir}/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<var name="env.PATH" value="some:custom:value"/>
You can use setx command to set the environment variables.
For setx command help refer http://ss64.com/nt/setx.html
<exec executable="setx.exe">
<arg line="Path C:\jdk1.5.0_12\bin"/>
<arg line="/m"/>
</exec>
I found it works by quote the value of variable
<exec executable="setx">
<arg line="Path "${env.Path};c:\testPath"" />
<arg line="/m" />
</exec>
You can use to expand the path:
And then you can execute for instance sh from ant to export the environment variable:
<property environment="env"/>
<exec executable="sh">
<arg value="-c"/>
<arg value="export PATH=${env.Path}:${myPath}"/>
</exec>
Or execute your command and set env with value, like so:
<property environment="env"/>
<exec executable="${your.command}">
<env key="PATH" value="${env.PATH}:${your.directory}"/>
</exec>
To set the environment variables through Ant, try calling exec task and set the command line values. I have not tried this by the way, but it should work.
since I don't have enough reputation to comment on the <variable ... suggestions my comment as an answer ... :-/
In ("newer") ant-contrib (extra ANT package) the task is not called <variable ... but <var ...!
(but it didn't work for me anyways since I think the manipulation of the env.* (created by <property environment="env" ... /> task) Java properties/variables is only relevant for tasks/processes evaluating these Java properties which are not automatically "synced back" to the OS environment variables)

ant script not expanding property value in exec arguments

i have an ant script as shown below:
<project name="nightly_build" default="main" basedir="checkout">
<target name="init">
<exec executable="C:/Work/Searchversion.exe"/>
<property file="initial.properties"/>
<property file="C:/Work/lastestbuild.properties"/>
<tstamp>
<format property="suffix" pattern="yyyyMMddHHmmss"/>
</tstamp>
</target>
<target name="main" depends="init">
<exec executable="C:/Program Files/True Blue Software/SnapshotCM/wco.exe">
<arg line='-h sinsscm01.sin.ds.net -S"/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}" /'/>
</exec>
</target>
</project>
i created the above script to replicate a command: wco -h sinsscm01.sin.ds.net -S"/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.014" /
and 6.70.102.014 is found inside latestbuild.properties file in the form of:
Version_Number = 6.70.102.014
and this latestbuild.properties file is obtained when i execute C:/Work/Searchversion.exe
but when i execute this ant script using cruisecontrol, in my log file,
[Thread-24] INFO ScriptRunner - [exec] Cannot open snapshot 'sinsscm01.sin.ds.jdsu.net:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}': No such snapshot
where ${Version_Number} should have been 6.70.102.014
How do i tackle this issue?
EDIT 1:
after trial and error and substituting with a built in property ${ant.version}, i realise that my property file could be loaded in correctly over here. can anyone point out my mistake? i dont see anything wrong though
EDIT 2:
Just additional infomation... This is actually a delegate ant script for cruisecontrol(used to perform nightly build). Here is my config.xml file for per minute build:
<cruisecontrol>
<project name="dms" buildafterfailed="true">
<listeners>
<currentbuildstatuslistener file = "logs/dms/status.txt"/>
</listeners>
<bootstrappers>
</bootstrappers>
<modificationset quietperiod="60">
<alwaysbuild/>
</modificationset>
<schedule interval="60">
<ant buildfile="nightly_build.xml" target="main"/>
</schedule>
<log dir="logs/dms">
<merge dir="checkout/dms/build/test-results" />
</log>
<publishers>
</publishers>
</project>
</cruisecontrol>
should properties file be loaded in config.xml?
Try breaking your arguments to wco.exe into separate child elements like this:
<exec executable="C:/Program Files/True Blue Software/SnapshotCM/wco.exe">
<arg value="-h" />
<arg value="sinsscm01.sin.ds.net" />
<arg value="-S" />
<arg value="/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}" />
<arg value="/" />
</exec>
I think ant isn't expanding ${Version_Number} because it is inside ' "..." ' in the version you posted.
As mentioned in the docs for <exec> you should avoid use of the <arg line=...> form.
You could add assertions in your init target that the required properties file exists and that the property is defined. For example:
<property name="version.file" value="C:/Work/lastestbuild.properties"/>
<available file="${version.file}" property="version.file.available"/>
<fail unless="version.file.available" message="file [${version.file}] is not available"/>
<property file="${version.file}"/>
<fail unless="version" message="property [version] is not defined"/>
<echo message="version: ${version}"/>
I think that will help you spot that the file does not exist.
I took a look at your other question about this script you're putting together. In the code which writes the version number to file, you use filename latestbuild.properties:
TextWriter latest = new StreamWriter("C:\\Work\\latestbuild.properties");
In your Ant script, you are loading a different filename lastestbuild.properties.
Unless you've fixed it since then, that will be your problem. (If you modified your external script to take the filename as a parameter, and defined the filename once as an Ant property - as in my sample above - it would help you avoid this kind of problem.)
Regarding your discovery that you need to wait for your external script before continuing in Ant, take a look at the Sleep task.

How can I execute a VBScript file from Ant script?

I want to run a VBScript file from an Ant script. How can I do that?
Have a look at the exec task. It allows you to execute a system command from your Ant script.
EDIT:
An example could be:
<target name="RunVbScript">
<exec executable="cscript">
<arg value="MyScript.vbs"/>
<arg value="argument 1" />
<arg value="argument 2" />
<arg value="argument n" />
</exec>
</target>

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"' />

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