0

I have a file called versionInfo.txt. This file among other things has the following text: "Implementation-Version: 7.5.0.1".

I need to retrieve the version value and copy this version value to a Java file. The Java file will have the following variable:

version = "@version-info@";

I need to replace this @version-info@ with the value I retrieved from the first file. I need to do plug in this code in an existing build.xml file written using ant script.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1088035
  • 9
  • 1
  • 4

2 Answers2

4

Create a properties file like this and name it build.properties

version.label=7.5.0.1

Then in your build.xml file

<project basedir=".">

    <target name="replace-labels">

        <property file="${basedir}/build.properties"/>

        <replace
            file="${basedir}/myClass.java"
            token="@version-info@"
            value="${version.label}" />

     </target>

</project>

So your file structure should look like

myproject
    build.properties
    build.xml
    myClass.java

Then you can execute your ANT build by changing to the "myproject" directory and executing

ant replace-labels

The replace tag will look for the string "@version-info@" in your myClass.java file and replace it with the value "7.5.0.1"

Brad
  • 15,186
  • 11
  • 60
  • 74
  • Hi Brad, thanks alot for the quick response. Could you also give me the code for reading the versionInfo.txt file, retrieve the version value and store it in a property as you have done above. I am new to ant and am not aware of the syntax etc. Would really appreciate if you could help out. Thanks – user1088035 Dec 08 '11 at 16:23
  • Can I use something like this to replace something from `html` file? I am trying to replace value of `href` from `` – Saurabh Bayani Jun 14 '16 at 13:37
0

For the second part of your question, retrieve the version info.. : If you need to read the Implementation-Version from the Manifest of a jar you may use a macrodef, f.e. :

<!-- Grep a keyvalue from Manifest -->
<macrodef name="mfgrep">
  <attribute name="jar"/>
  <attribute name="key"/>
  <attribute name="catch"/>
    <sequential>
      <loadproperties>
        <zipentry zipfile="@{jar}" name="META-INF/MANIFEST.MF"/>
      </loadproperties>
        <property name="@{catch}" value="${@{key}}"/>
    </sequential>
</macrodef>

 <mfgrep
   jar="/home/rosebud/temp/ant.jar"
   key="Implementation-Version"
   catch="foobar"
 />

<echo>$${foobar} => ${foobar}</echo>
Rebse
  • 10,307
  • 2
  • 38
  • 66