enter code hereI want to increment my version from property file in ant build.xml
I am using below code.
It is able to increment the version but at the same time it is rounding it eg. 4.1.0 is becoming 5. my property file:
buildversion=4.1.0
my code:
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
<propertyfile file="build.properties">
<entry key="buildversion" type="int" operation="+" value="1"/>
</propertyfile>
</target>
</project>
I read about propertyfile and it support only int,date and string.
How I am able to do it?
Added fields for major.minor.release-build and timestamp:
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
<!-- Declare, set and increment the values -->
<propertyfile file="build.properties">
<entry key="buildmajor" type="int" default="0"/>
<entry key="buildminor" type="int" default="0"/>
<entry key="buildrelease" type="int" default="0"/>
<entry key="buildbuild" type="int" default="0" operation="+" value="1"/>
<!-- ISO timestamp -->
<entry key="buildtime" type="date" value="now" pattern="yyyy.MM.dd HH:mm:ss"/>
</propertyfile>
<!-- Re-read values -->
<property file="build.properties"/>
<!-- Set calculated value based on re-read values -->
<propertyfile file="build.properties">
<entry key="buildversion"
value="${buildmajor}.${buildminor}.${buildrelease}-${buildbuild}"/>
</propertyfile>
</target>
Edited the above snippet to re-read changed values before calculating version string.
Also added some comments...
Related
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 execute an ant xml file using jenkins. I have to update the versionbuild.txt file information before making war.
my ant tasks are init, clean, prepare, compile, war
before making war, I want to access some parameters like date_of_build, build_number, jdk_compiler, compiler_platform, build_person. Update these in text file named versionbuild.txt file and archive into war.
I am doing something similar in one of my build scripts. Basically creating a properties file which I place as a resource in the source folder. Here's the snippet:
<target name="init">
<!-- Format the build number to a 4 figures integer with leading zeroes if required -->
<!-- The build number is also incremented by 1 -->
<propertyfile file="${build.number.file}">
<entry key="build.number" type="int" default="0000" operation="+" pattern="0000" />
</propertyfile>
<!-- Read the build number -->
<property file="${build.number.file}"/>
<!-- create the build date and time with time zone -->
<tstamp>
<format property="readable.now" pattern="yyyy-MM-dd hh:mm z"/>
</tstamp>
<!-- Write thebuild and version information to a resource file -->
<!-- The resource file will be part of the corresponding jar -->
<propertyfile file="${node}/src/resources/META-INF/jppf-version.properties">
<entry key="version.number" type="string" value="${version.number}"/>
<entry key="build.number" type="string" value="${build.number}"/>
<entry key="build.date" type="string" value="${readable.now}"/>
</propertyfile>
</target>
The resulting file looks like this:
#Wed, 30 Apr 2014 07:45:12 +0200
version.number=4.2 alpha
build.number=1352
build.date=2014-04-30 07\:45 CEST
Obviously, some of the data you need is not in there, but you can easily add them as <entry> elements, using the name of the corresponding system properties as the value, for instance:
<propertyfile file="${path.to}/versionbuild.txt">
<entry key="build_number" type="string" value="${build.number}"/>
<entry key="date_of_build" type="string" value="${readable.now}"/>
<entry key="jdk_compiler" type="string" value="${java.vm.version}"/>
<entry key="jdk_compiler" type="string" value="${os.name}"/>
<entry key="build_person" type="string" value="${user.name}"/>
</propertyfile>
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.
I'm in a situation that involves running an ant build with optional parameters that are always specified but but not always defined, like so
ant -DBUILD_ENVIRONMENT=test -Dusername_ext= -Dconf.dir_ext= -Dcgi-dir_ext=
If the parameters are not given values on the command line they will be by loading a .properties file. I have the following code that will check if the property isset and is not blank.
<if>
<bool>
<and>
<isset property="username_ext"/>
<not>
<equals arg1="${username_ext}" arg2="" />
</not>
</and>
</bool>
<then>
<property name="username" value="${username_ext}" />
</then>
</if>
<property file="${BUILD_ENVIRONMENT}.properties" />
Since there are multiple properties it seems like I should write a target that will do the same actions for each property rather than repeat that code every time.
<antcall target="checkexists">
<property name="propname" value="username"/>
<property name="paramname" value="username_ext"/>
</antcall>
<antcall target="checkexists">
<property name="propname" value="conf.dir"/>
<property name="paramname" value="conf.dir_ext"/>
</antcall>
But AFAIK an antcall will not set a global property. How then can I write a target that will accept the name of a parameter it needs to check is set and is not blank, and then copy that in to a parameter that other targets can use?
Rather than using a target you could use a macro to conditionally set properties based on whether or not another property is set to a non-empty value.
<macrodef name="set-property">
<attribute name="name" />
<attribute name="if-property-isset" />
<attribute name="value" default="${#{if-property-isset}}" />
<sequential>
<condition property="#{name}" value="#{value}">
<and>
<isset property="#{if-property-isset}" />
<not>
<equals arg1="${#{if-property-isset}}" arg2="" />
</not>
</and>
</condition>
</sequential>
</macrodef>
<target name="test-macro">
<set-property name="username" if-property-isset="username_ext" />
<set-property name="conf.dir" if-property-isset="conf.dir_ext" />
<property name="conf.dir" value="default conf directory" />
<echo message="username = ${username}" />
<echo message="conf.dir = ${conf.dir}" />
</target>
Output
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml
test-macro:
[echo] username = jsmith
[echo] conf.dir = default conf directory
BUILD SUCCESSFUL
Total time: 1 second
Alternate Property Value
This macro also allows you set the property to a different value than the one provided on the command line.
<target name="test-macro">
<set-property name="username" if-property-isset="username_ext"
value="It worked!" />
<set-property name="conf.dir" if-property-isset="conf.dir_ext" />
<property name="conf.dir" value="default conf directory" />
<echo message="username = ${username}" />
<echo message="conf.dir = ${conf.dir}" />
</target>
Output
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml
test-macro:
[echo] username = It worked!
[echo] conf.dir = default conf directory
BUILD SUCCESSFUL
Total time: 1 second
I am working on an Ant build process for an application that uses a versioning in the following format: major.minor.buildcount. So currently the application is around 2.1.52, where we are on version 2.1 and there have been 35 builds.
I am now adding in an ant target to ask the user if they would like to advance the major version and/or the minor version.
When I run my target from the command line I would like to follow the following:
## ant version
Versioning application...
Would you like to advance the major version to 3? (Y|n)
## n
Not Advancing major version
Would you like to advance the minor version to 2? (y|N)
## y
Advancing minor version
The lines prepended with ## is the user input that I would like to take. My major and minor versions are stored in a build.properties file.
Here is my code so far
<?xml version="1.0"?>
<project name="StudentMS" default="zip" basedir=".">
<propertyfile file="./.ant/build.properties">
<entry key="version.buildnumber" type="int" default="0" operation="+" pattern="00" />
</propertyfile>
<property file="./.ant/build.properties" />
<property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" />
<property name="buildDir" location="${sourceDir}/builds" />
<target name="version" description="Adds a major and minor version to the build.">
<input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" />
<propertyfile file="./.ant/build.properties">
<entry key="version.major" type="int" default="0" operation="+" pattern="00" />
</propertyfile>
<input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" />
<propertyfile file="./.ant/build.properties">
<entry key="version.minor" type="int" default="0" operation="+" pattern="00" />
</propertyfile>
</target>
</project>
And my build.properties
#Tue, 29 Mar 2011 11:46:30 -0400
version.buildnumber=35
version.major=2
version.minor=1
I am still very new to Ant so I am sorry that I can't post more advanced code. So the first thing I need to do is add some kind of conditional around my property file edits.
what you want can be achieved by combining the condition and antcall task and by adding a couple of extra targets.
I think something like this should work:
<property file="./.ant/build.properties" />
<property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" />
<property name="buildDir" location="${sourceDir}/builds" />
<target name="version" description="Adds a major and minor version to the build.">
<input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" />
<condition property="executeMajor">
<and>
<isset property="updatemajor" />
<equals arg1="${updatemajor}" arg2="y" />
</and>
</condition>
<antcall target="update_major" />
<input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" />
<condition property="executeMinor">
<and>
<isset property="updateminor" />
<equals arg1="${updateminor}" arg2="y" />
</and>
</condition>
<antcall target="update_minor" />
</target>
<target name="update_major" if="executeMajor">
<!-- Code to update major here -->
</target>
<target name="update_minor" if="executeMinor">
<!-- Code to update minor here -->
</target>
Basically, what it does is set the executeMajor and executeMinor properties just in the case that the updatemajor/updateminor are set to "y". Then, ant will run the update targets just if the executeMajor/Minor variables are set, and it will skip them otherwise.
An alternative would support both user input and an unattended build.
You can define ant properties on the command line. So, when you want to advance a version, you could do something like this:
ant -Dbuild.version.advanceMinor=true
This approach would also allow you to avoid the extra steps on the majority of builds.