Kaushalpanchal42
Kaushalpanchal42

Reputation: 5

Parsing xml property to ant-build.xml file

I have a xml file "versionreferance.xml". below is the coding

<?xml version="1.0" encoding="utf-8"?>
<!-- Update "revision" value to -1 when the next build is intended to have its revision as 0. This needs to be done for patch/release builds-->
<BuildVersions>
  <property name="major" value="1" />
  <property name="minor" value="0" />
  <property name="patch" value="0" />
  <property name="revision" value="93" />
</BuildVersions>

The other is my ant-build.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project name="xmlproperty-demo" default="init">
    <xmlproperty file="versionreferance.xml" collapseAttributes="true" />
        <echo>Major : ${BuildVersions.major}</echo>
        <echo>Minor : ${BuildVersions.minor}</echo>
        <echo>Patch : ${BuildVersions.patch}</echo>
        <echo>Revision : ${BuildVersions.revision}</echo>
</project>

I am not getting the value in the output.

I want to get the path path dynamically of versionReference.xml , which will be in different place than antbuild.xml

Development\Build\VersionReference.xml

Development\Src\JAAS API\antbuild.xml

It should be dynamically. I am able to get the values form below code. But it's not dynamic.

<?xml version="1.0" encoding="UTF-8"?>
<project name="xmlproperty-demo" default="init">
    <xmlproperty file="C:\\Development\\Build\\versionreferance.xml" collapseAttributes="true" />
        <echo>Major : ${BuildVersions.major}</echo>
        <echo>Minor : ${BuildVersions.minor}</echo>
        <echo>Patch : ${BuildVersions.patch}</echo>
        <echo>Revision : ${BuildVersions.revision}</echo>
</project>

I want to get the values only in this format.

<?xml version="1.0" encoding="utf-8"?>
<!-- Update "revision" value to -1 when the next build is intended to have its revision as 0.     This needs to be done for patch/release builds-->
<BuildVersions>
  <property name="major" value="1" />
  <property name="minor" value="0" />
  <property name="patch" value="0" />
  <property name="revision" value="93" />
</BuildVersions>

Upvotes: 0

Views: 4280

Answers (4)

B Bhatnagar
B Bhatnagar

Reputation: 1806

If VersionReference.xml is going to be exactly where you mentioned in your question corresponding to your antbuild.xml.

I would suggest you to change your Absolute Path (as mentioned in question) with "..\..\Build\VersionReference.xml" which corresponds to the basedir="." in your project

This should work fine.

And considering your string format requirement.

I think the default comma(,) seperated value can then be replaced by dot(.) or manipulated as per your requirement by the following syntax

<propertyregex property="propB"
               input="${propA}"
               regexp=","
               replace="."
               global="true" />

Please find more info on this link : Replacing characters in Ant property

Hope this helps.

Upvotes: 0

Chad Nouis
Chad Nouis

Reputation: 7041

I highly recommend using the free, third-party XmlTask library:

build.xml

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmltask>
    <fileset file="versionreferance.xml"/>
    <copy path="/BuildVersions/property[@name = 'major']/@value" property="BuildVersions.major"/>
    <copy path="/BuildVersions/property[@name = 'minor']/@value" property="BuildVersions.minor"/>
    <copy path="/BuildVersions/property[@name = 'patch']/@value" property="BuildVersions.patch"/>
    <copy path="/BuildVersions/property[@name = 'revision']/@value" property="BuildVersions.revision"/>
</xmltask>

<echo>Major : ${BuildVersions.major}</echo>
<echo>Minor : ${BuildVersions.minor}</echo>
<echo>Patch : ${BuildVersions.patch}</echo>
<echo>Revision : ${BuildVersions.revision}</echo>

Output

run:
     [echo] Major : 1
     [echo] Minor : 0
     [echo] Patch : 0
     [echo] Revision : 93

Upvotes: 2

user3139488
user3139488

Reputation:

When you say xmlproperty with collapseAttributes="true" on xml file, it will flatten nested elements.

Details: https://ant.apache.org/manual/Tasks/xmlproperty.html

So just saying ${BuildVersions.major} doesn't give or print value as 1. Since you have same named xml element 'prooerty' you won't be able to get single propertie's value

For eg, in your case ${BuildVersions.property.name} will print 'major,minor,patch,revision'

Either you can redesign your versionreferance.xml to respect xmlproperty to work . Or you can make it as property file & say <property file="your.properties.file"/> & access them as normal properties!

Upvotes: 1

bgossit
bgossit

Reputation: 1086

If you add <echoproperties/> after the <xmlproperty> line you should see the properties are loaded but not as you expect. They will be something like:

BuildVersions.property=,,,
BuildVersions.property.name=major,minor,patch,revision
BuildVersions.property.value=1,0,0,93

This is because the properties are based off the node names, and in your case you have four instances of a "property" child node that are not unique. Your best bet may be to change the xml such as:

<BuildVersions>
    <major>1</major>
    ....
</BuildVersions>

Upvotes: 1

Related Questions