views:

113

answers:

1

What I'm trying to do is compile to a file which takes it's version from a constant inside my source files.

I have a setup like this (or at least the significant bits):

tasks/compile.xml

<project name="Compile">
    <target name="check.version">
     <script language="javascript">
            regex = /VERSION.+?"([\d\.]+)";/;
            r = new java.io.BufferedReader(new java.io.FileReader(new java.io.File(file)));
            line = r.readLine();
            while ( (line = r.readLine()) != null) {
                m = regex.exec(line);
                if (m) {
              project.setProperty( "project.version" , m[1] );
           break;
                }
            }
            r.close();
        </script>
     <echo>${ant.project.name}-${project.version}</echo> <!-- = Fail-0.2 -->
    </target>
</project>

And a build.xml:

<project name="Fail"> 
    <import file="${basedir}/build/tasks/compile.xml"/>

    <target name="test">
     <antcall target="check.version">
      <param name="file" value="${basedir}/Source.as"/>
     </antcall>
     <echo>${project.version}</echo> <!-- = ${project.version} -->
     <echoproperties></echoproperties>
    </target>
</project>

So, it seems that the property set by the script is only locally defined in that target, if i specify another target in the same project ("Compile") it won't know of that property either.

I've also tried to set a in the "Compile"-project xml but it won't be overwritten by the target anyway.

So how can I access that property generated by the script? Or is there another way for doing something like this?

I'd really like to keep that part in a separate xml as it makes the project build script so much cleaner.

Thanks!

+1  A: 

If you call the other target via antcall, properties set within it won't be in the scope of the caller.

If you need to access properties set by another target, you could declare that target as a dependency to ensure that it gets executed before your target. Like this:

<target name="test" depends="check.version">
    <echo>${project.version}</echo> <!-- = value set in check.version -->
    <echoproperties></echoproperties>
</target>


Edit: There's also the AntCallBack task which is available from Ant-Contrib and Antelope:

AntCallBack is identical to the standard 'antcall' task, except that it allows properties set in the called target to be available in the calling target.

Jukka Matilainen
Excellent! But as I can't pass a parameter into a depends (or can I?) I simply look for a property then. Thanks!
Robert Sköld
Edited the answer to add a mention of `antcallback` task from Ant-Contrib.
Jukka Matilainen