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
Related
I have successfully integrate Apache ant & Configured it. I am using Jmeter for load testing.
Now I was trying to generate HTML report of load testing and it was working fine until I have deleted test.jmx and test.html was there in folder C:\apache-ant-1.9.6\bin
But as my test plan name and JTL files name is different , I have deleted above test.jmx and test.html and specified name in build.xml:
testplan ="${testpath}/${mytestplanname}.jmx"
resultlog="${testpath}/${mytest}.jtl">
But now after completing load testing when I run ant command then it says :
Cound not found C:\apache-ant-1.9.6\bin\test.jmx
No sure why it is still finding test file , it should find name which I have specified in in built.xml.
I want this ant script to generate HTML report of my current test rather than it's "TEST.jmx" plan
BUILD.XML is given here :
<?xml version="1.0"?>
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
Sample build file for use with ant-jmeter.jar
See http://www.programmerplanet.org/pages/projects/jmeter-ant-task.php
To run a test and create the output report:
ant -Dtest=script
To run a test only:
ant -Dtest=script run
To run report on existing test output
ant -Dtest=script report
The "script" parameter is the name of the script without the .jmx suffix.
Additional options:
-Dshow-data=y - include response data in Failure Details
-Dtestpath=xyz - path to test file(s) (default user.dir).
N.B. Ant interprets relative paths against the build file
-Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file)
-Dreport.title="My Report" - title for html report (default is 'Load Test Results')
Deprecated:
-Dformat=2.0 - use version 2.0 JTL files rather than 2.1
</description>
<property name="testpath" value="${user.dir}"/>
<property name="jmeter.home" value="${basedir}/.."/>
<property name="report.title" value="Load Test Results"/>
<!-- Name of test (without .jmx) -->
<property name="test" value="Test"/>
<!-- Should report include response data for failures? -->
<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>
<!-- Allow jar to be picked up locally -->
<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"/>
<jmeter
jmeterhome="${jmeter.home}"
testplan ="${testpath}/${test}.jmx"
resultlog="${testpath}/${test}.jtl">
<!--
<jvmarg value="-Xincgc"/>
<jvmarg value="-Xmx128m"/>
<jvmarg value="-Dproperty=value"/>
<jmeterarg value="-qextra.properties"/>
-->
<!-- Force suitable defaults -->
<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"/>
<!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ -->
<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,copy-images">
<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${style_version}.xsl">
<param name="showData" expression="${show-data}"/>
<param name="titleReport" expression="${report.title}"/>
<param name="dateReport" expression="${report.datestamp}"/>
</xslt>
</target>
<!-- Copy report images if needed -->
<target name="copy-images" depends="verify-images" unless="samepath">
<copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/>
<copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/>
</target>
<target name="verify-images">
<condition property="samepath">
<equals arg1="${testpath}" arg2="${basedir}" />
</condition>
</target>
<!-- Check that the xalan libraries are present -->
<condition property="xalan.present">
<and>
<!-- No need to check all jars; just check a few -->
<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>
Note :
1 - My all tests are in path : C:\apache-jmeter-2.13\apache-jmeter-2.13\bin
2 - Above build.xml is in path : C:\apache-jmeter-2.13\apache-jmeter-2.13\extras
You can run Apache Ant against your .jmx files using build.xml file which lives under /extras folder of your JMeter installation without having to copy or delete anything, just provide location and .jmx file name via -D command-line arguments like:
ant -Dtestpath=/path/to/the/folder/with/test -Dtest=testname.without.jmx.extension
Given your JMeter script lives i.e. in c:\tests\mytest.jmx
You need to launch Ant as follows:
ant -Dtestpath=c:/tests -Dtest=mytest
and it will generate the following files:
C:\tests\mytest.jtl
C:\tests\mytest.html
References:
JMeter Ant Task
Five Ways To Launch a JMeter Test without Using the JMeter GUI
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"/>
I have the antcontrib.jar in my lib folder of Ant. I set my ant home as "C/Prog Files/apache-ant".
But still when I run my build.xml, i get the warning "could not load antlib.xml and antcontrib.prop".
Because of this, I am not able to do any "regex" operations.
I properly loaded the antcontrib.jar in the lib folder of the ant.
Where I am wrong here?
Provide resource and classpath in your taskdef correctly as follows
<typedef resource="net/sf/antcontrib/antlib.xml" classpath="<path to ant-contrib.jar>"/>
Here's an example of an Ant script that uses Ant-Contrib's <propertyregex> task:
build.xml
<project name="ant-propregex-simple" default="run">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<target name="run">
<property name="line.to.test" value="First Second" />
<property name="the.regex" value="^([^ ]*) ([^ ]*)$" />
<propertyregex
input="${line.to.test}"
regexp="${the.regex}"
select="\2"
property="the.match"
/>
<echo>${the.match}</echo>
</target>
</project>
The key is the <taskdef ...> line.
Output
run:
[echo] Second
BUILD SUCCESSFUL
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.
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!