Is there a way of conditionally redirecting the output to property or the stdout stream as the non-working example below?
<macrodef name="mytask">
<attribute name="output" default="STDOUT"/>
<sequential>
<exec executable="my.exe" outputproperty="#{output}"/>
</sequential>
</macrodef>
The above example redirects the output by default to a property STDOUT. Instead I would like it to be directed to the stdout stream.
I could create mytask_with_stdout as a copy of the above macro and remove the exec outputproperty, but that would violate the DRY principle.
Is there some nice way of doing this?
There are two Ant features you can combine to get what you want.
First, a <macrodef> can be passed whatever <element> you want.
Second, a <redirector> can be used to capture the output of an <exec> command in a property.
I ran the following Ant script on a Windows machine so I could use cmd.exe's echo command. Replace the cmd.exe with your my.exe:
<project name="exec-redirector-example" default="run">
<macrodef name="mytask">
<attribute name="message"/>
<element name="myredirector" optional="true"/>
<sequential>
<exec executable="cmd.exe">
<arg value="/c"/>
<arg value="echo"/>
<arg value="#{message}"/>
<myredirector/>
</exec>
</sequential>
</macrodef>
<target name="run">
<!-- exec outputs to STDOUT by default -->
<mytask message="To STDOUT">
</mytask>
<!-- exec outputs to a property in this example -->
<mytask message="To property">
<myredirector>
<redirector outputproperty="my.property"/>
</myredirector>
</mytask>
<echo>${my.property}</echo>
</target>
</project>
Related
I have a got a ANT build system which invokes builds on different projects using following macro;
<macrodef name="buildComponent">
<attribute name="name"/>
<attribute name="dir"/>
<attribute name="antTarget"/>
<attribute name="antCommonDistDir" />
<sequential>
<available property="build.xml.exists.#{dir}" file="#{dir}/build.xml" />
<if>
<equals arg1="${build.xml.exists.#{dir}}" arg2="true" />
<then>
<java classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="#{dir}"
timeout="4000000"
output="${common.build.dir}/log/#{name}.log"
taskname="startAnt" >
<jvmarg value="-Dant.home=${ant.home}"/>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-Dbasedir=#{dir}"/>
<arg value="#{antTarget}"/>
<arg value="-Dprop1=${prop1}" />
<syspropertyset refid="project.common.properties" />
<sysproperty key="common.dist.dir.os" value="#{antCommonDistDir}" />
</java>
</then>
</if>
</sequential>
</macrodef>
I would like to override properties form command line but the problem is that these properties are not being passed by task and my subsequent build uses the default values. For example I am executing the build as follows;
ant dist -Dprop1=override.prop1 -Dprop2=override.prop2 -Dprop3=override.prop3
As you see currently the only option for me to pass these overridden values from command line for prop2 and prop3 is add <arg /> under <java /> task for each property passed like I have done for 'prop1' which works but not desirable. Is there anyway I can access all properties passed to ANT and simply pass them as is to <java /> task?
You can use the echoproperties task to save all current Ant properties to a file, and then pass that file to the java task to be loaded by the subproject.
<echoproperties destfile="my.properties"/>
Having said this, a better solution instead of executing the java command to invoke another Ant build, you can simply call the ant task which will build your subproject and automatically inherit all properties from the parent project:
<available property="build.xml.exists.#{dir}" file="#{dir}/build.xml" />
<if>
<equals arg1="${build.xml.exists.#{dir}}" arg2="true" />
<then>
<ant antfile="#{dir}/build.xml" target="#{antTarget}"/>
</then>
</if>
I couldn't find any thing which can do this directly. So I ended up writing a javascript and populated a ant property to parse command line options which was stored in env variable and passed it to <java /> task as <arg line="${command.line.properties}" />. Remember to use <arg line="" /> as it trim out all extra spaces etc before invoking a 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.
How can i overwrite some existing property with a newly created properties file?
Here is the required structure:
initially load Master.properties
generate new.properties
load new.properties and master.properties
run master.xml (ANT script)
The idea is that Master.properties generates some product version which should be replaced by new.properties. However, other properties in Master.properties should be kept the same.
Reading this does not help as i do not know how can i load the new.properties file
EDIT Here is ANT Script:
<project name="nightly_build" default="main" basedir="C:\Work\NightlyBuild">
<target name="init1">
<sequential>
<property file="C:/Work/NightlyBuild/master.properties"/>
<exec executable="C:/Work/Searchlatestversion.exe">
<arg line='"/SASE Lab Tools" "${Product_Tip}/RELEASE_"'/>
</exec>
<sleep seconds="10"/>
<property file="C:/Work/new.properties"/>
</sequential>
</target>
<target name="init" depends="init1">
<sequential>
<echo message="The product version is ${Product_Version}"/>
<exec executable="C:/Work/checksnapshot.exe">
<arg line='-NightlyBuild ${Product_Version}-AppsMerge' />
</exec>
<sleep seconds="10"/>
<property file="C:/Work/checksnapshot.properties"/>
<tstamp>
<format property="suffix" pattern="yyyy-MM-dd.HHmm"/>
</tstamp>
</sequential>
</target>
<target name="main" depends="init">
<echo message="loading properties files.." />
<echo message="Backing up folder" />
<move file="C:\NightlyBuild\NightlyBuild" tofile="C:\NightlyBuild\NightlyBuild.${suffix}" failonerror="false" />
<exec executable="C:/Work/sortfolder.exe">
<arg line="6" />
</exec>
<exec executable="C:/Work/NightlyBuild/antc.bat">
</exec>
</target>
</project>
in the above script, <exec executable="C:/Work/NightlyBuild/antc.bat"> will run Master.xml ANT script. This Master.xml will load up Master.properties:
<project name="Master ANT Build" default="main" >
<taskdef name="CFileEdit" classname="com.ANT_Tasks.CFileEdit"/>
<!-- ========================================================== -->
<!-- init: sets global properties -->
<!-- ========================================================== -->
<target name="init">
<property environment="env"/>
<!-- ========================================================== -->
<!-- Set the timestamp format -->
<!-- ========================================================== -->
<property file="Master.properties"/>
...
</project>
You should be able to resolve this by looking at the order in which you load (or otherwise specify) your property values. You probably don't need to override property values at all, which something not supported by core Ant.
Maybe you can split your Master.properties into two files - one loaded before you generate new.properties and one loaded after?
Maybe you don't need to generate new.properties at all.
Could you give some more detail on what you need to do?
Since you eventually fork a new Ant process (exec antc.bat), does that not start a fresh environment anyway? If it just loads Master.properties, those are the only properties it will have.
Not sure what your antc.bat does, but it's pretty unusual to exec Ant from Ant in this way. There are two standard tasks which might be useful - Ant and AntCall.
OK running on from your later comments...
Let's say that instead of doing this:
<exec executable="antc.bat">
you instead did something like this:
<ant file="Master.xml" inheritall="false">
<property name="Product_Version" value="${Product_Version}"/>
</ant>
I think that is getting towards what you want. You selectively pass specific values that you have obtained by loading new.properties. See the documentation for the Ant task.
If you still have the problem that you already defined Product_Version before loading new.properties, then I would say get the script you have that produces new.properties to output the version with a different name, e.g. New_Product_Version. Then invoke your master build something like this:
<ant file="Master.xml" inheritall="false">
<property name="Product_Version" value="${New_Product_Version}"/>
</ant>
May be this is a old question. Hopefully OP is reading this.
You can just use the ant task "propertyfile". reference
it can read properties from the file and write back updated values to them.
I need to execute an ant task for each line from a given file. Any ideas appreciated.
I have a properties file that defines processes that need to be run. This is all on a single line and comma separated, not multiple lines as you have specified. This answer shows how to iterate over a file.
env.start=webserver:localhost, dataserver:localhost
then in my ant file that handles application execution I have the following
<target name="start-all" description="Start all processes specified in target-info.properties:env.start">
<foreach list="${env.start}" trim="yes" param="process.and.host" target="-start-process"/>
</target>
<target name="-start-process">
<property name="colon.separated.pattern" value="([^:]*):([^:]*)"/>
<propertyregex property="process" input="${process.and.host}" regexp="${colon.separated.pattern}" select="\1"/>
<propertyregex property="host" input="${process.and.host}" regexp="${colon.separated.pattern}" select="\2"/>
<condition property="start.target" value="start-${process}" else="-start-process-ssh">
<equals arg1="${host}" arg2="localhost" trim="yes" casesensitive="no"/>
</condition>
<antcall target="${start.target}"/>
</target>
${start.target} then is executed for the processes defined in the env.start property for example
<target name="start-webserver" description="Start the webserver on this machine">
<echo>** Starting webserver **</echo>
<run-script dir="${project.base.dir}/apache-tomcat" script="${project.base.dir}/apache-tomcat/bin/startup" spawn="yes">
<args>
<env key="CATALINA_HOME" value="${project.base.dir}/apache-tomcat"/>
<env key="CATALINA_PID" value="${project.base.dir}/apache-tomcat/logs/pid_catalina"/>
</args>
</run-script>
</target>
<target name="start-dataserver" depends="decipher_caldb_password,check-event-seed,run-prestart-sql" description="Start the dataserver on this machine">
<run-calypso process="dataserver" class="calypsox.apps.startup.StartNOGUI" failonerror="yes"
args="-class com.calypso.apps.startup.StartDataServer"/>
</target>
I can start all the processes defined in env.start by running
ant -f run.xml start-all
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"