how to update buildversion.txt file after build using ant and jenkins - ant

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>

Related

how to add float value from properties file in ant

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...

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?

echo message does not show me the correct value from properties file

I have a local.properties file, which contains only one value:
student.id=100
I created the following Ant script to increase the value of student.id by 1 :
<target name="increase-id">
<!-- student.id is increased by 1, no problem -->
<propertyfile file="local.properties">
<entry key="student.id" type="int" operation="+" value="1" />
</propertyfile>
<!--The following echo always show me "Student ID: 1, why?"-->
<echo message="Student ID: ${student.id}"/>
</target>
Every time after I run command ant increase-id , the value of student.id in local.properties file is increased by 1. No problem here. But, the <echo> message always show me Student ID: 1
Why?
This one works for me:
<project name="increase.test" default="increase-id">
<target name="increase-id">
<!-- documentation says that this task is for editing property files -->
<propertyfile file="local.properties">
<entry key="student.id" type="int" operation="+" value="1" />
</propertyfile>
<!-- so you should load this property after edit -->
<property file="local.properties" />
<echo message="Student ID: ${student.id}"/>
</target>
</project>
By the way don't use relative paths. Instead of this always use absolute paths. If you want to load property file which is in the same directory as build.xml you can use <dirname/> task.
<dirname property="build.dir" file="${ant.file.<projectName>}"/>,
where:
${ant.file} is builtin property which indicates to currently runned build.xml
<projectName> is just project name
Try to always create dirname's using this pattern ${ant.file.<projectName> because if you will use only ${ant.file} it will indicates to main build. for example if you have build which runs other build and in the other build you will use ${ant.file} it will indicates to the main build. If it is not clear just read Apache ant documentation and try to create simple build.

sonar ant build.xml file for running default Sun checks on Java project

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.

Asking user for yes|no input

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.

Resources