ant > map property names into a new set of properties - ant

I'd like to "map" a bunch of ant properties, based on a prefix (sounds simple enough).
I have a solution, but it's not elegant (having to write out to a properties file, then read it back in!)
Question: Is there a quicker/more generic/simpler/out-of-the-box/straight-forward way of doing the below "load-propertyset" within ANT? (... than the example I've provided below)
(Roughly analogous to the Groovy > ConfigSlurper > Special Environment Configuration behaviour.)
For example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Config">
<!-- Section 1. (These will be loaded from a property file...) -->
<property name="a.yyy" value="foo" />
<property name="a.zzz" value="cat" />
<property name="b.xxx" value="bar" />
<property name="b.zzz" value="dog" />
<macrodef name="load-propertyset">
<attribute name="prefix" />
<attribute name="outfile" default="123" />
<attribute name="propset" default="123" />
<sequential>
<propertyset id="#{propset}">
<propertyref prefix="#{prefix}" />
<globmapper from="#{prefix}.*" to="*" />
</propertyset>
<echo level="debug">Created propertyset - '#{propset}' from prefix='#{prefix}'</echo>
<tempfile property="#{outfile}" suffix=".properties" deleteonexit="true" />
<echo level="debug">Writing propset to temp file - '${#{outfile}}'</echo>
<echoproperties destfile="${#{outfile}}">
<propertyset refid="#{propset}"/>
</echoproperties>
<echo level="debug">Reading props from temp file - '${#{outfile}}'</echo>
<property file="${#{outfile}}" />
<delete file="${#{outfile}}" />
</sequential>
</macrodef>
<load-propertyset prefix="a" />
<load-propertyset prefix="b" />
<echo>>>> Using variables xxx=${xxx} yyy=${yyy} zzz=${zzz}</echo>
</project>
I'm sure I'm missing something simple, for instance:
Can I reference properties within a propertyset? (e.g. ${myprops.yyy} ?)
I'd like to avoid something like ${${filter}.hostname}.

The third-party Ant-Contrib has an <antcallback> task that takes the <antcall> task and adds the ability to return properties to the caller:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Config" default="run">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Section 1. (These will be loaded from a property file...) -->
<property name="a.yyy" value="foo" />
<property name="a.zzz" value="cat" />
<property name="b.xxx" value="bar" />
<property name="b.zzz" value="dog" />
<macrodef name="load-propertyset">
<attribute name="prefix" />
<attribute name="return" />
<sequential>
<antcallback target="-empty-target" return="#{return}">
<propertyset>
<propertyref prefix="#{prefix}" />
<globmapper from="#{prefix}.*" to="*" />
</propertyset>
</antcallback>
</sequential>
</macrodef>
<target name="-empty-target"/>
<target name="run">
<property name="properties-to-return" value="xxx,yyy,zzz"/>
<load-propertyset prefix="a" return="${properties-to-return}"/>
<load-propertyset prefix="b" return="${properties-to-return}"/>
<echo>>>> Using variables xxx=${xxx} yyy=${yyy} zzz=${zzz}</echo>
</target>
</project>
The properties-to-return concept is a bit of a maintenance burden. Luckily, <antcallback> doesn't fail when asked to return a property that wasn't set. Only the properties-to-return property needs to be modified when you want a new mapped property.

Related

ColdFusion TestBox Ant error

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?

how to use fileset in ant macrodef

I would like to use the fileset in below macrodef.
I wish to change attribute jar to dir so that all jar files in dir can be processed.
<macrodef name="unjartemp">
<attribute name="jar" />
<sequential>
<!-- Remove any existing signatures from a JAR file. -->
<tempfile prefix="unjar-"
destdir="${java.io.tmpdir}" property="temp.file" />
<echo message="Removing signatures from JAR: #{jar}" />
<mkdir dir="${temp.file}" />
<unjar src="#{jar}" dest="${temp.file}" />
<delete file="#{jar}" failonerror="true" />
</sequential>
</macrodef>
To keep it flexible you may use macrodef with nested element attribute for 1-n filesets, f.e.
a macrodef that creates a dirlisting in xmlformat for nested filesets :
<macrodef name="dir2xml">
<attribute name="file"
description="xmlfile for filelisting"/>
<attribute name="roottag"
description="xml root tag"/>
<attribute name="entrytag"
description="xml tag for entry"/>
<element name="fs"
description="nested filesets for listing"/>
<sequential>
<pathconvert
property="files.xml"
dirsep="/"
pathsep="</#{entrytag}>${line.separator} <#{entrytag}>"
>
<!-- 1-n nested fileset(s) -->
<fs/>
</pathconvert>
<!-- create xmlfile -->
<echo message="<#{roottag}>${line.separator} <#{entrytag}>${files.xml}</#{entrytag}>${line.separator}</#{roottag}>" file="#{file}"/>
</sequential>
</macrodef>
Usage :
<dir2xml file="filelistant.xml" entrytag="antfile" roottag="antfilelist">
<fs>
<fileset dir="." includes="**/*.xml"/>
<fileset dir="../ant_xml" includes="**/*.xml"/>
</fs>
</dir2xml>
Add your fileset:
<fileset dir="${jars.dir}" id="jars_to_unjar">
<include name="**/*.jar"/>
</fileset>
call you macros:
<unjartemp filesetref="jars_to_unjar"/>
And you can try this modified macros:
<macrodef name="unjartemp">
<attribute name="filesetref" />
<sequential>
<for param="file">
<fileset refid="#{filesetref}"/>
<sequential>
<!-- Remove any existing signatures from a JAR file. -->
<tempfile prefix="unjar-"
destdir="${java.io.tmpdir}" property="temp.file" />
<echo message="Removing signatures from JAR: #{file}" />
<mkdir dir="${temp.file}" />
<unjar src="#{file}" dest="${temp.file}" />
<delete file="#{file}" failonerror="true" />
</sequential>
</for>
</sequential>
</macrodef>

Configuration of build.xml file for Sonar modules with Ant

I am setting up a Sonar project (using the delphi plugin), for simplicity sake assume there are two modules I want to report on.
Each module is in it's own sub-folder and each has it's own build.xml file.
At this point I can successfully run the sonar tasks and generate reports for each module as an independent project.
My problem is with configuring the "master" build.xml file.
The module build.xml file looks like this:
<?xml version="1.0"?>
<project name = "CRM" default = "sonar" basedir = ".">
<!-- Add the Sonar task -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<target name="sonar">
<property name="sonar.projectKey" value="EXO:CRM" />
<property name="sonar.host.url" value="http://localhost:9000" />
<sonar:sonar workDir="." key="CRM.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
<property key="sonar.sources" value="." /> <!-- project sources directories (required) -->
<property key="sonar.language" value="delph" /> <!-- project language -->
<property key="sonar.delphi.codecoverage.excluded" value=".\tests" /> <!-- code coverage excluded directories -->
<property key="sonar.importSources" value="true" /> <!-- should we show sources or not? -->
<property key="sonar.delphi.sources.excluded" value="" /> <!-- excluded directories -->
<property key="sonar.delphi.sources.include" value=".\includes" /> <!-- include directories, "," separated -->
<property key="sonar.delphi.sources.include.extend" value="true" /> <!-- should we extend includes in files? -->
</sonar:sonar>
</target>
</project>
The "Master" build.xml looks like this:
<?xml version="1.0"?>
<project name = "EXO" default = "sonar" basedir = ".">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<target name="sonar">
<property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
<sonar:sonar workDir="." key="EXO.key" version="0.1" xmlns:sonar="antlib:org.sonar.ant">
<!-- project sources directories (required) -->
<property key="sonar.sources" value="." />
<property key="sonar.language" value="delph" />
<property key="sonar.importSources" value="true" />
<property key="sonar.delphi.sources.excluded" value="" />
<property key="sonar.delphi.sources.include" value=".\includes" />
<property key="sonar.delphi.sources.include.extend" value="true" />
</sonar:sonar>
</target>
</project>
It is always scanning all sources (required value) i.e. it is not respecting my modules.
The only way I can get this to work currently is by limiting the source code like this
<property key="sonar.sources" value="./crm/,./exonet6000/" />
I'm sure I must be mis-configuring something obvious here.
EDIT: I have now what I believe is a more consistent set of files based on examples here https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/java-ant-modules
Master build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name = "EXO" default = "sonar" basedir = "." xmlns:sonar="antlib:org.sonar.ant">
<echo>Root Project</echo>
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="c:/ANT/lib" />
</taskdef>
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.modules" value="exonet6000/build.xml,CRM/build.xml" />
<target name="sonar">
<sonar:sonar key="EXO.key" version="0.1">
</sonar:sonar>
</target>
</project>
and one of the submodule files
<?xml version="1.0" encoding="UTF-8"?>
<project name="CRM" default="all" basedir=".">
<echo>CRM Module</echo>
<property name="sonar.language" value="delph" />
<property name="sonar.projectKey" value="EXO:CRM" />
<property name="sonar.sources" value="." />
<target name="all" />
</project>
At this point the sonar process is completing successfully BUT no actual anlysis is being done. A key point is that I am not seeing the echo of the submodule so I suspect these build tasks are not actually running.
If you look at this sample project using Ant and multimodules, I'd say that you should not specify any property inside the tag in your master build.xml file, and let the submodules specify those properties.
I've got a project with many submodules. My sonar target looks like this:
<target name="sonar" depends="build.dependencies" description="collect code metrics">
<property name="m1" value="a/build.xml,b/build.xml,c/build.xml,d/build.xml"/>
... more properties defining modules...
<property name="sonar.modules" value="${m1},${m2},${m3},${m4},${m5},${m6}..."/>
<property name="sonar.projectName" value="MyProject"/>
<sonar:sonar key="my:project" version="${version}" xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>
</target>
No property definitions in the sonar:sonar task at all, and it is working as I want it to.
Sources are defined in the submodule build.xml files. (Actually, in a base-build.xml that all our build.xml files include... but that's an Ant thing not directly related to Sonar.)

sorting as well as removing duplicacy

<?xml version="1.0"?>
<project name="sortlist11" default="sortlist11">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<property name="my.list" value="z,y,x,w,v,u,t" />
<property name="my.list1" `value="5,3,6,1,8,4,6" `/>
<target name="sortlist11">
<sortlist property="my.sorted.list" value="${my.list}" delimiter="," />
<sortlist property="my.sorted.list1" value="${my.list1}" delimiter="," />
<echo message="${my.sorted.list}" />
<echo message="${my.sorted.list1}" />
</target>
</project>
here second echo print 1,3,4,5,6,6,8 but how can i remove redundancy?
Every language running in JVM via Bean Scripting Framework may be used in ant with full access to the ant api. Here's a solution with Groovy for your problem =
<project>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<property name="my.list" value="z,y,x,w,v,u,t"/>
<property name="my.list1" value="5,3,6,1,8,4,6"/>
<groovy>
properties.'my.sorted.list' = properties.'my.list'.split(',').sort().toString()
properties.'my.sorted.list1' = properties.'my.list1'.split(',').toList().unique().sort().toString()
</groovy>
<echo>
$${my.sorted.list} => ${my.sorted.list}
$${my.sorted.list1} => ${my.sorted.list1}
</echo>
</project>

ant-contrib - if/then/else task

I am using ant, and I have a problem with if/then/else task, (ant-contrib-1.0b3.jar).
I am running something that can be simplified with build.xml below.
I am expecting to obtain from 'ant -Dgiv=Luke' the message
input name: Luke
should be overwritten with John except for Mark: John
but it seems property "giv" is not overwritten inside if/then/else..
input name: Luke
should be overwritten with John except for Mark: Luke
Is it depending from the fact I am using equals task with ${giv} ?
Otherwise what is wrong in my code?
build.xml CODE:
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
In Ant a property is always set once, after that variable is not alterable anymore.
Here follows a solution using standard Ant (without ant-contrib) which could be useful for the people who does not want an extra dependency.
<target name="test" >
<echo message="input name: ${param}" />
<condition property="cond" >
<equals arg1="${param}" arg2="Mark" />
</condition>
</target>
<target name="init" depends="test" if="cond">
<property name="param2" value="Mark" />
</target>
<target name="finalize" depends="init">
<property name="param2" value="John" />
<echo message="should be overwritten with John except for Mark: ${param2}" />
</target>
Ant Properties are very hard to overwrite (if not impossible). What you need is a Variable. These are also defined in the Ant Contrib JAR.
Editing your example:
<target name="ifthen">
<var name="Evangelist" value="${giv}" />
<echo message="input name: ${Evangelist}" />
<if>
<equals arg1="${Evangelist}" arg2="Mark" />
<then>
</then>
<else>
<var name="Evangelist" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${Evangelist}" />
</target>
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<var name="giv" unset="true"/>
<property name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
We can use var task to unset the property also.
I know this is old, but should prove handy to others searching for a solution.
to re-assign a property without using ant-contrib, use macrodef with a script.
<macrodef name="property-change">
<attribute name="name"/>
<attribute name="value"/>
<sequential>
<script language="javascript"><![CDATA[
project.setProperty("#{name}", "#{value}");
]]></script>
</sequential>
</macrodef>
then in anywhere in ant, just call this like the property tag
<property-change name="giv" value="John"/>
to Implement this in your original version of xml, it would look like this:
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property-change name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
This sample is given purely as an example on writing a macro to replace the <var> command in ant-contrib. In a situation like this one, where the <if> command is being used, it makes more sense to use <var> sinnce ant-contrib is already loaded, and <var> might be faster in processing.
Hope this helps.
It is possible to re-assign the value of a property using the ant-contrib 'propertycopy'. This is an alternative to using ant-contrib Variables.
This way the property "giv" can be overwritten.
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property name="tempName" value="John" />
<propertycopy name="giv" from="tempName" override="true" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
Be aware this assumes the property tempName is not already set to a value other than 'John'.

Resources