javaxmlantxml-parsingantbuilder

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>

Solution

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