0

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?

john123
  • 589
  • 3
  • 6
  • 16

1 Answers1

1

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:

  1. ${ant.file} is builtin property which indicates to currently runned build.xml
  2. <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.

pepuch
  • 6,346
  • 7
  • 51
  • 84
  • I tried it too. It does not work for me. I still always get 1 as the student id from echo – john123 Mar 18 '13 at 14:02
  • What ant version have you got? You need to remember that you shouldn't load files in this way ``. You should create absolute paths for this as I wrote here http://stackoverflow.com/questions/15419471/in-ant-how-do-i-specify-files-with-comma-in-filename/15420111#15420111 (``) – pepuch Mar 18 '13 at 14:03
  • Apache Ant version 1.8.0 compiled on April 9 2010 – john123 Mar 18 '13 at 14:04
  • Could you please tell me how to use in my case, I tried something, but it does not help either. – john123 Mar 18 '13 at 14:14
  • I think the reason is that the property is immutable – john123 Mar 18 '13 at 14:25
  • See my edit. Yes, apache ant properties are immutable. Because of that you should load property `student.id` after editing the file. Can you paste your build, I'll try to figure where is the problem? – pepuch Mar 18 '13 at 14:45