开发者

Can't access property generated by a script in an imported ANT target

开发者 https://www.devze.com 2022-12-11 09:39 出处:网络
What I\'m trying to do is compile to a file which takes it\'s version from a constant inside my source files.

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 sig开发者_运维百科nificant 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消