English 中文(简体)
Can t access property generated by a script in an imported ANT target
原标题:

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!

最佳回答

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.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签