I have a property file containing keys and values as shown below:
TC_name1=true
Tc_name2=false
I need an Ant script to iterate through this property file and get only the keys with value equal to true.
Also I want to know whether it is possible to use an .ini file as our property file.
Please help me out with this as I googled, but was not able to find the solution.
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project default="load">
<target name="load">
<!-- load the properties as plain text into a property -->
<loadfile property="trueProps" srcfile="load.properties">
<!-- only load affirmative values of properties -->
<filterchain>
<linecontainsregexp>
<regexp pattern="=\s*(true|yes|on)\s*$" />
</linecontainsregexp>
</filterchain>
<!-- prefix for easy retrieval -->
<filterchain>
<prefixlines prefix="testing." />
</filterchain>
</loadfile>
<!-- print the property contents -->
<echo taskname="trueProps">${trueProps}</echo>
<!-- extract the keys -->
<loadresource property="trueKeys" >
<string>${trueProps}</string>
<filterchain>
<replaceregex pattern="=\s*(true|yes|on)\s*$" replace="" />
</filterchain>
</loadresource>
<!-- print the keys -->
<echo taskname="trueKeys">${trueKeys}</echo>
<!-- load the contents of the variable as ant properties -->
<loadproperties>
<string>${trueProps}</string>
</loadproperties>
<!-- confirm the properties are loaded as expected -->
<echoproperties prefix="testing." />
</target>
</project>
load.properties:
prop1=false
some_other=true
also_true=yes
yet_another=on
not_this=no
TC_name1=true
Tc_name2=false
some_other_match =yes
not_a_yes= false
output:
Buildfile: build.xml
load:
[trueProps] testing.some_other=true
[trueProps] testing.also_true=yes
[trueProps] testing.yet_another=on
[trueProps] testing.TC_name1=true
[trueProps] testing.some_other_match =yes
[trueKeys] testing.some_other
[trueKeys] testing.also_true
[trueKeys] testing.yet_another
[trueKeys] testing.TC_name1
[trueKeys] testing.some_other_match
[echoproperties] #Ant properties
[echoproperties] #Wed May 29 10:57:26 EDT 2013
[echoproperties] testing.TC_name1=true
[echoproperties] testing.also_true=yes
[echoproperties] testing.some_other=true
[echoproperties] testing.some_other_match=yes
[echoproperties] testing.yet_another=on
BUILD SUCCESSFUL
Total time: 0 seconds
Related
I'm trying to display an ant patternset for debugging purposes. In my sample the patternset is displayed as dontcompile patternSet{ includes: [] excludes: [] }, no info from the excludesfile is shown.
I know the excludesfile is working because on Linux no windows sources are shown at the end.
build.xml
<project default="init" >
<condition property="osname" value="linux">
<os family="unix" />
</condition>
<!-- if it's not unix assume it's some kind of windows -->
<property name="osname" value="windows" />
<target name="init" >
<local name="dont_compile.os.present" />
<local name="dont_compile.present" />
<available file="dont_compile.${osname}.lst" property="dont_compile.os.present" />
<available file="dont_compile.lst" property="dont_compile.present" />
<echo>dont_compile.${osname}.lst ${dont_compile.os.present}${line.separator}dont_compile.lst ${dont_compile.present}</echo>
<patternset id="dontcompile">
<excludesfile name="dont_compile.${osname}.lst" if="dont_compile.os.present" />
<excludesfile name="dont_compile.lst" if="dont_compile.present" />
</patternset>
<fileset id="myfileset" dir=".">
<include name="**/*.source" />
<patternset refid="dontcompile" />
</fileset>
<loadfile property="os_contents" srcFile="dont_compile.${osname}.lst" />
<local name="ps.value" />
<property name="ps.value" refid="dontcompile" />
<echo>
dontcompile ${ps.value}"
os_contents ${os_contents}
${toString:myfileset}
</echo>
</target>
</project>
contents of dont_compile.linux.lst
windows.source
win/**
contents of my test directory
./build.xml
./dont_compile.linux.lst
./general.source
./linux.source
./windows.source
./win/sub.source
sample output
Buildfile: build.xml
init:
[echo] dont_compile.linux.lst true
[echo] dont_compile.lst ${dont_compile.present}
[echo]
[echo] dontcompile patternSet{ includes: [] excludes: [] }"
[echo] os_contents windows.source
[echo] win/sub.source
[echo]
[echo] general.source;linux.source
BUILD SUCCESSFUL
Total time: 0 seconds
My goal is to run TestBox scripts on Jenkins. But using the Ant script from
https://testbox.ortusbooks.com/content/running_tests/ant_runner.html
as a template, I get this error
BUILD FAILED
C:\public\data\trunk\AutomatedTesting\Box_Unit_Tests\build.xml:38: The reference to entity "bundles" must end with the ';' delimiter.
with this script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="testbox-ant-runner" default="init" basedir=".">
<!-- THE URL TO THE RUNNER, PLEASE CHANGE ACCORDINGLY-->
<property name="basedir" value="C:\public\data\trunk\AutomatedTesting\Box_Unit_Tests" />
<property name="url.runner" value="C:\public\data\ColdBox\testbox\test-harness\runner.cfm?"/>
<!-- FILL OUT THE BUNDLES TO TEST, CAN BE A LIST OF CFC PATHS -->
<property name="test.bundles" value="http://localhost/application/testing/TestBox/Hello.cfc?method=runRemote" />
<!-- FILL OUT THE DIRECTORY MAPPING TO TEST -->
<property name="test.directory" value="test.specs" />
<!-- FILL OUT IF YOU WANT THE DIRECTORY RUNNER TO RECURSE OR NOT -->
<property name="test.recurse" value="true" />
<!-- FILL OUT THE LABELS YOU WANT TO APPLY TO THE TESTS -->
<property name="test.labels" value="" />
<!-- FILL OUT THE TEST REPORTER YOU WANT, AVAILABLE REPORTERS ARE: ANTJunit, Codexwiki, console, dot, doc, json, junit, min, raw, simple, tap, text, xml -->
<property name="test.reporter" value="simple" />
<!-- FILL OUT WHERE REPORTING RESULTS ARE STORED -->
<property name="report.dir" value="${basedir}\results" />
<property name="junitreport.dir" value="${report.dir}\junitreport" />
<target name="init" description="Init the tests">
<mkdir dir="${junitreport.dir}" />
<tstamp prefix="start">
<format property="TODAY" pattern="MM-dd-YYYY hh:mm:ss aa"/>
</tstamp>
<concat destfile="${report.dir}\Latestrun.log">Tests ran at ${start.TODAY}</concat>
</target>
<target name="run">
<get dest="${report.dir}/results.html"
src="${url.runner}&bundles=${test.bundles}&reporter=${test.reporter}"
verbose="true"/>
<-- Create fancy junit reports -->
<junitreport todir="${junitreport.dir}">
<fileset dir="${report.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junitreport.dir}">
<param name="TITLE" expression="My Awesome TestBox Results"/>
</report>
</junitreport>
</target>
</project>
Any thoughts?
I have a properties file that I want to echo out the values
<HSMKeys>
<Key domain="default" value="TestA" />
<Key domain="fixed" value="TestB" />
</HSMKeys>
as
[echo] domain=default value=TestA
[echo] domain=fixed value=TestB
How can I do this, ie loop over the properties file and have the two variables that would be in the echo.
I have tried the following
<for list="${HSMKeys.Key.domain}" param="domain">
<sequential>
<echo>domain=#{domain}</echo>
</sequential>
</for>
ie I can only get at one attribute value at a time, not both at once.
Thanks.
As #thekbb has stated this is not a properties file, however, ANT supports the parsing of XML files as properties.
<project name="demo" default="print">
<xmlproperty file="properties.xml"/>
<target name="print">
<echoproperties prefix="HSMKeys."/>
</target>
</project>
Produces the following output:
print:
[echoproperties] #Ant properties
[echoproperties] #Tue Dec 03 23:44:14 GMT 2013
[echoproperties] HSMKeys.Key=,
[echoproperties] HSMKeys.Key(domain)=default,fixed
[echoproperties] HSMKeys.Key(value)=TestA,TestB
May not be exactly what you need but has the advantage of not requiring additional jars like Ant-contrib.
That's not a properties file. Properties files are key/value pairs. How are you populating list?
a property file ant will understand will look like:
domain.default=TestA
domain.fixed=testB
I recommend avoiding ant-contrib at all costs... what are you really trying to do?
you could simply use echoproperties
<project name="test" default="echo" basedir=".">
<property file="build.properties" />
<target name="echo" >
<echoproperties prefix="domain."/>
</target>
</project>
I have a list of strings (e.g. "piyush,kumar") in an Ant script for which I want to assign piyush to var1 like <var name="var1" value="piyush"/> and kumar to var2 like <var name="var2" value="kumar"/>.
So far, I'm using a buildfile like the following:
<?xml version="1.0"?>
<project name="cutter" default="cutter">
<target name="cutter">
<for list="piyush,kumar" param="letter">
<sequential>
<echo>var1 #{letter}</echo>
</sequential>
</for>
</target>
</project>
I'm not sure how to progress this - any suggestions?
Here's an example using an ant-contrib variable and the math task:
<var name="index" value="1"/>
<for list="piyush,kumar" param="letter">
<sequential>
<property name="var${index}" value="#{letter}" />
<math result="index" operand1="${index}" operation="+" operand2="1" datatype="int" />
</sequential>
</for>
<echoproperties prefix="var" />
Output:
[echoproperties] var1=piyush
[echoproperties] var2=kumar
This is all very un-Ant like though - once you've set these what are you going to do with them?
You might consider using an Ant script task instead for this sort of non-declarative processing.
I am a beginner to SONAR , i just need a help for a sample ant build file for running my java project name 'Hello World' with SONAR 's default Sun checks Quality profile .I have not found anywhere any proper ant guide for sonar. I am using SONAR 2.10 .
Please help me in starting with SONAR .
<project name="Example" default="Sonar" basedir=".">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\Program Files\Apache Software Foundation\ant\lib\sonar-ant-task-1.0.jar" />
</taskdef>
<!-- Out-of-the-box those parameters are optional -->
<property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3309/sonar" />
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<property name="sonar.jdbc.username" value="root" />
<property name="sonar.jdbc.password" value="root" />
<!-- Additional Sonar configuration (PMD need 1.5 when using annotations)-->
<property name="sonar.java.source" value="1.5"/>
<property name="sonar.java.target" value="1.5"/>
<property name="sonar.projectName" value="Example"/>
<property name="sonar.binaries" value="C:\Documents and Settings\tausif\Feature2\Example\bin"/>
<!-- SERVER ON A REMOTE HOST -->
<property name="sonar.host.url" value="http://localhost:8080/sonar" />
<target name="Sonar">
<!-- The workDir directory is used by Sonar to store temporary files -->
<sonar:sonar workDir="C:\Documents and Settings\tausif\Feature2\Sonar" key="com.example:example" xmlns:sonar="antlib:org.sonar.ant" >
<!-- source directories (required) -->
<sources>
<path location="C:\Documents and Settings\tausif\Feature2\Example" />
</sources>
</sonar:sonar>
</target>
</project>
The above two answers were realy helpful for me to create this xml file .
This is my sample build.xml . Can you please check what i am missing in it?
I have made Sun checks as default.My project name is Example.
You might find this (Sonar 2.6: Adds Continuous Inspection Support for Ant Community) or this (Analyse with Ant Task 1.0) documentation helpful.
You can refer below ant script which is specific to the sonar.
You can add it in your build.xml.
Below is the script with the details
<!-- Here you need to set the path which contains sonar specific jars required for ant e.g. path which contains sonar-ant-task-2.1.jar -->
<path id="sonar.classpath">
<fileset dir="${basedir}/sonar" includes="**/*.jar" />
</path>
<!-- This taskdef represents your ant lib for sonar you have to specify jar location along with jar name in class path no need to change the uri and resource-->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${basedir}\sonar\sonar-ant-task-2.1.jar" />
</taskdef>
<!-- This is the target we use to run sonar "depends" property is optional -->
<target name="sonar" depends="clean, compile">
<!-- specify your build version -->
<property name="build.version" value="0.0.0.1-Sonar"/>
<!-- specify your organization name its optional -->
<property name="mysonar.organizationName" value="XYZ"/>
<!-- specify your project Name -->
<property name="sonar.projectName" value="${project.name}" />
<!-- database url which is used by the sonar -->
<property name="sonar.jdbc.url" value="jdbc:mysql://<IP>:<Port>/sonar?useUnicode=true&characterEncoding=utf8" />
<!-- Driver name-->
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<!-- database user name -->
<property name="sonar.jdbc.username" value="test" />
<!-- database password -->
<property name="sonar.jdbc.password" value="test" />
<!-- url on which sonar is running-->
<property name="sonar.host.url" value="http://<IP>:<Port>" />
<!-- project key -->
<property name="sonar.projectKey" value="${mysonar.organizationName}:${sonar.projectName}" />
<!-- project version-->
<property name="sonar.projectVersion" value="1.0" />
<!-- location source files -->
<property name="sonar.sources" value="${src.home}/main/java" />
<!-- location of binaries after compilation-->
<property name="sonar.binaries" value="${basedir}/output"/>
<!-- location of sonar library-->
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>
</target>
Note: Make sure that location you specify are correct you can give absolute path as well.