Override ignored for property - ant

Content of Build.xml File
<?xml version="1.0"?>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="ant-contrib-0.6.jar"/>
<taskdef classpath="orangevolt-ant-tasks-1.3.2.jar" resource="com/orangevolt/tools/ant/taskdefs.properties"/>
<project name="initinstaller" default="all" basedir="." >
<target name="configure-server-types-module">
<property file="./installation.conf"/>
<echo message="${client.server.types}"/>
<if>
<not>
<contains string="${client.server.types}" substring="tomcat" />
</not>
<then>
<replaceregexp file="./installation.conf"
match="client.server.types=(.*)"
replace="client.server.types=\1tomcat,"
byline="true">
</replaceregexp>
</then>
</if>
</target>
<target name="all" depends="configure-server-types-module">
<property file="./installation.conf"/>
<echo message="${client.server.types}"/>
<if>
<not>
<contains string="${client.server.types}" substring="tomcat" />
</not>
<then>
<replaceregexp file="./installation.conf"
match="client.server.types=(.*)"
replace="client.server.types=\1tomcat,"
byline="true">
</replaceregexp>
</then>
</if>
</target>
Content of installation.conf : client.server.types=jboss,
Verbose Output :
Apache Ant version 1.8.1 compiled on April 30 2010
Trying the default build file: build.xml
Buildfile: D:\testing\build.xml
Detected Java version: 1.6 in: C:\Program Files\Java\jdk1.6.0_21\jre
Detected OS: Windows XP
parsing buildfile D:\testing\build.xml with URI = file:/D:/testing/build.xml
Project base dir set to: D:\testing
parsing buildfile jar:file:/C:/apache-ant-1.8.1/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/C:/apache-ant-1.8.1/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
dropping D:\testing\ant-contrib-0.6.jar from path as it doesn't exist
dropping D:\testing\orangevolt-ant-tasks-1.3.2.jar from path as it doesn't exist
Build sequence for target(s) `all' is [configure-server-types-module, all]
Complete build sequence is [configure-server-types-module, all, ]
configure-server-types-module:
[property] Loading D:\testing\installation.conf
[echo] jboss,
[replaceregexp] Replacing pattern 'client.server.types=(.*)' with
'client.server.types=\1tomcat,' in 'D:\testing\installation.conf' by line.
[replaceregexp] File has changed; saving the updated file
all:
[property] Loading D:\testing\installation.conf
**Override ignored for property "client.server.types"**
[echo] jboss,
[replaceregexp] Replacing pattern 'client.server.types=(.*)' with
'client.server.types=\1tomcat,' in 'D:\testing\installation.conf' by line.
[replaceregexp] File has changed; saving the updated file
BUILD SUCCESSFUL
Total time: 0 seconds
==============================================================
my question is that how i can override the ant property

Once an Ant property has been set its value cannot be changed.
Perhaps you could use the prefix attribute of the <property> task when you first load the properties file so that the property has a different name:
<target name="configure-server-types-module">
<property file="./installation.conf" prefix="temp."/>
<echo message="${temp.client.server.types}"/>
<if>
<not>
<contains string="${temp.client.server.types}" substring="tomcat" />
</not>
<then>
<replaceregexp file="./installation.conf"
match="client.server.types=(.*)"
replace="client.server.types=\1tomcat,"
byline="true">
</replaceregexp>
</then>
</if>
</target>
Note: I haven't tested this.
Then in the second target you can still use the proper name as that property has not been set yet.
(this solution only works with 3rd-party plugin ANT-contrib-library installed: ANT contrib Download)

If you're already using the Ant-Contrib jar as it seems from your example, you could change your property definition to a var (see http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html)

Use
<property name="x" value="6"/>
<echo>${x}</echo> <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo> <!-- will print 12 -->
It's work for my fine!

Related

Generating HTML report with Ant 1.9.7 from Jmeter 3.0 file - "Start and end within the same entity" Error

Here is basically the build.xml that Jmeter 3.0 generated itself. When I try to run the tests with 40 000 users within an hour I get the error after 17 minutes - "${path}/build.xml: Fatal error during transformation using ${path}/jmeter-results-detail-report_21.xsl: XML document structures must start and end within the same entity.; SystemID: file:${path}/Test.jtl;"
<?xml version="1.0"?>
<project name="ant-jmeter" default="all">
<property name="testpath" value="${user.dir}"/>
<property name="jmeter.home" value="${basedir}/.."/>
<property name="report.title" value="Load Test Results"/>
<property name="target.report.dir" location="Asjad/apache-jmeter-3.0/extras/report"/>
<property name="test" value="Test"/>
<property name="show-data" value="n"/>
<property name="format" value="2.1"/>
<condition property="style_version" value="">
<equals arg1="${format}" arg2="2.0"/>
</condition>
<condition property="style_version" value="_21">
<equals arg1="${format}" arg2="2.1"/>
</condition>
<condition property="funcMode">
<equals arg1="${show-data}" arg2="y"/>
</condition>
<condition property="funcMode" value="false">
<not>
<equals arg1="${show-data}" arg2="y"/>
</not>
</condition>
<path id="jmeter.classpath">
<fileset dir="${basedir}">
<include name="ant-jmeter*.jar"/>
</fileset>
</path>
<taskdef
name="jmeter"
classpathref="jmeter.classpath"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<target name="all" depends="run,report"/>
<target name="run">
<echo>funcMode = ${funcMode}</echo>
<delete file="${testpath}/${test}.html"/>
<delete file="${testpath}/${test}.jtl"/>
<jmeter
jmeterhome="${jmeter.home}"
testplan ="${testpath}/${test}.jmx"
resultlog="${testpath}/${test}.jtl">
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="jmeter.save.saveservice.bytes" value="true"/>
<property name="file_format.testlog" value="${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/>
</jmeter>
</target>
<property name="lib.dir" value="${jmeter.home}/lib"/>
<path id="xslt.classpath">
<fileset dir="${lib.dir}" includes="xalan*.jar"/>
<fileset dir="${lib.dir}" includes="serializer*.jar"/>
</path>
<target name="report" depends="xslt-report">
<echo>Report generated at ${report.datestamp}</echo>
</target>
<target name="xslt-report" depends="_message_xalan">
<tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp>
<xslt
classpathref="xslt.classpath"
force="true"
in="${testpath}/${test}.jtl"
out="${testpath}/${test}.html"
style="${basedir}/jmeter-results-detail-report_21.xsl">
<param name="showData" expression="${show-data}"/>
<param name="titleReport" expression="${report.title}"/>
<param name="dateReport" expression="${report.datestamp}"/>
</xslt>
</target>
<condition property="xalan.present">
<and>
<available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/>
<available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/>
</and>
</condition>
<target name="_message_xalan" unless="xalan.present">
<echo>Cannot find all xalan and/or serialiser jars</echo>
<echo>The XSLT formatting may not work correctly.</echo>
<echo>Check you have xalan and serializer jars in ${lib.dir}</echo>
</target>
</project>
It is a simple test that just makes requests to the webpage.
I am using Ant build 1.9.7 and Jmeter 3.0.
I can think of 2 possible reasons:
Ant might be trying to convert .jtl to HTML when the .jtl is incomplete. In order to work that around:
Add the next line to <jmeter> section:
<property name="jmeter.save.saveservice.autoflush" value="true"/>
Or alternatively put add the following line to user.properties file (lives in JMeter's "bin" folder)
jmeter.save.saveservice.autoflush=true
It will "tell" JMeter to store the results as soon as they arrive.
40 000 users is quite a "heavy" load and JMeter default configuration might not be suitable for this. I believe you need to add at least some Java Heap space. In case of Ant it would be adding the next lines to <jmeter> section:
<jvmarg value="-Xmx8G"/>
Change that 8G reference value to be around 80% of your available physical RAM. See 9 Easy Solutions for a JMeter Load Test “Out of Memory” Failure guide for more recommendations on JMeter tuning for maximum performance
Showing your jmeter.log file (jmeter 3.0 will tell you where it is generated) will help.
But you may be facing this bug if using ANT + XSL to generated report:
https://bz.apache.org/bugzilla/show_bug.cgi?id=59918
See this :
Generating a faulty report when running JMeter 3.0 test with Ant

Conditionally load properties files in Ant

My Ant script should download a ZIP file which contains a set-up file to be installed in a database (Oracle or PostgreSQL) and generate dumps. Different dumps are generated depending on the properties data provided in the set up file.
I have 3 properties files:
user.properties : this contains various details provided from Jenkins and apart from that a value: prepare.MTdump.generate=true
nonMT.properties
MT.properties
Is it possible in Ant to load the first properties file user.properties and depending upon the condition (e.g. if prepare.MTdump.generate=true) load MT.properties or if it's false load nonMT.properties?
I have been unable to add an IF condition to load the properties file. I even tried with the unless condition of <target> but have been unable to achieve the requirement.
If you are using ant-contrib, this should work:
<property file="user.properties"/>
<if>
<equals arg1="${prepare.MTdump.generate}" arg2="true"/>
<then>
<property file="MT.properties"/>
</then>
<else>
<property file="nonMT.properties"/>
</else>
</if>
Otherwise, you can just use conditions. Just run the loadProperties target below.
<property file="user.properties"/>
<target name="test.if.use.MT">
<condition property="useMT">
<equals arg1="${prepare.MTdump.generate}" arg2="true"/>
</condition>
<condition property="useNonMT">
<not>
<equals arg1="${prepare.MTdump.generate}" arg2="true"/>
</not>
</condition>
</target>
<target name="loadMTProperties" if="${useMT}" depends="test.if.use.MT">
<property file="MT.properties"/>
</target>
<target name="loadNonMTProperties" if="${useNonMT}" depends="test.if.use.MT">
<property file="nonMT.properties"/>
</target>
<target name="loadProperties" depends="loadMTProperties, loadNonMTProperties"/>

Available tag in ant is always true if a file is unavailable also

This code is always returning a true value even if file at given path does not exists
<available file="${x}/schema/#{componentname}-schema.sql" type="file" property="schema.file" />
<if>
<equals arg1="true" arg2="${schema.file}" />
<then>
<debug message="****schemafile is ${schema.file} ******" />
</then>
</if>
Output is always :-
*schemafile is true***
even if file is not available at that path.
Please help me to find the error.
I've refactored your example, in order to use standard ANT tasks:
<project name="demo" default="run" xmlns:if="ant:if">
<property name="src.dir" location="src"/>
<target name="run">
<available file="${src.dir}/schema/schema.sql" type="file" property="schema.file" />
<echo message="****schemafile is ${schema.file} ******" if:set="schema.file"/>
</target>
</project>
Notes:
I don't recognise the "debug" task so use the standard "echo" task instead
I recommend not using the ant-contrib "if" task. ANT 1.9.1 introduced an if attribute which can be used instead.
The following alternative variant will work with older versions of ANT. It uses an "if" target attribute to perform conditional execution:
<project name="demo" default="run">
<property name="src.dir" location="src"/>
<available file="${src.dir}/schema/schema.sql" type="file" property="schema.file" />
<target name="run" if="schema.file">
<echo message="****schemafile is ${schema.file} ******"/>
</target>
</project>
problem was i was iterating above code in for loop, and since property is immutable, it is always set to true if set at-least once. Thats why after 1 iteration even if the file was not found, it echoes schemafile is true** .
i have added below code to set property to false after that code
<var name="schema.file" unset="true"/>
<property name="schema.file" value="false"/>

how to write ant script to check a given string is a file or directory

how to use write ant script to check a file is a file or directory???
Given a string "C:\test\application\Services\Test"
I need to know this string
"C:\test\application\Services\Test"
is a file or directory,
I use following script to check, looks like cannot decide it is s a file or directory
<if>
<available file="${sourceFile}" />
<then>
<echo> It is a File ${sourceFile}</echo>
<copyfile src="${sourceFile}" dest="${targetFile}" />
<echo> Single file copied: sourceFile = ${sourceFile}</echo>
</then>
<else>
<if>
<available file="${dir}" type="dir" />
<then>
<echo> It is a Directory: ${sourceFile}</echo>
<copy todir="${targetFile}">
<fileset dir="${sourceFile}" />
</copy>
<echo> Single dir copied: sourceFile = ${sourceFile}</echo>
</then>
</if>
</else>
</if>
How to use ant to do it???
Thanks
The if/else tasks are not part of standard ANT (requires 3rd party jar)
Conditional execution of targets is performed as follows:
<project name="demo" default="process">
<property name="sourceFile" location="C:\test\application\Services\Test"/>
<available property="sourceFile.is.a.file" file="${sourceFile}" type="file"/>
<available property="sourceFile.is.a.dir" file="${sourceFile}" type="dir"/>
<target name="process" depends="process-file,process-dir"/>
<target name="process-dir" if="sourceFile.is.a.dir">
<echo message="${sourceFile} is a dir"/>
</target>
<target name="process-file" if="sourceFile.is.a.file">
<echo message="${sourceFile} is a file"/>
</target>
</project>

How to load Ant properties from property files on the command line?

I have two property files [one.properties and two.properties]. I want to dynamically load the property files into my Ant project from the command line.
My build file name is build.xml.
Command line:
> ant build [How do I pass the property file names here?]
Loading property files from the command line
ant -propertyfile one.properties -propertyfile two.properties
Individual properties may be defined on the command line with the -D flag:
ant -Dmy.property=42
Loading property files from within an Ant project
LoadProperties Ant task
<loadproperties srcfile="one.properties" />
<loadproperties srcfile="two.properties" />
Property Ant task
<property file="one.properties" />
<property file="two.properties" />
Match property files using a pattern
JB Nizet's solution combines concat with fileset:
<target name="init" description="Initialize the project.">
<mkdir dir="temp" />
<concat destfile="temp/combined.properties" fixlastline="true">
<fileset dir="." includes="*.properties" />
</concat>
<property file="temp/combined.properties" />
</target>
Making a build condition such that if Required System parameters are provided for build then only allowing for the next target else build gets fail.
Pass CMD: ant -DclientName=Name1 -Dtarget.profile.evn=dev
Fail CMD: ant
<project name="MyProject" default="myTarget" basedir=".">
<target name="checkParams">
<condition property="isReqParamsProvided">
<and>
<isset property="clientName" /> <!-- if provide read latest else read form property tag -->
<length string="${clientName}" when="greater" length="0" />
<isset property="target.profile.evn" /> <!-- mvn clean install -Pdev -->
<length string="${target.profile.evn}" when="greater" length="0" />
</and>
</condition>
<echo>Runtime Sytem Properties:</echo>
<echo>client = ${clientName}</echo>
<echo>target.profile.evn = ${target.profile.evn}</echo>
<echo>isReqParamsProvided = ${isReqParamsProvided}</echo>
<echo>Java/JVM version: ${ant.java.version}</echo>
</target>
<target name="failOn_InSufficentParams" depends="checkParams" unless="isReqParamsProvided">
<fail>Invalid params for provided for Build.</fail>
</target>
<target name="myTarget" depends="failOn_InSufficentParams">
<echo>Build Success.</echo>
</target>
</project>
#see also: Replace all tokens form file

Resources