2
votes

In our project we have a common.xml which is included in all our ant files. This common.xml has all common tasks we use in our project.

Recently we wanted to use ant-contrib in our project. so we have included following task def in our common.xml

<project name="CommonTasks" basedir="." xmlns:ac="antlib:net.sf.antcontrib">
    <taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml>
        <classpath>
            <pathelement location="/usr/lib/ant-contrib-1.0b3.jar" />
       </classpath>
    </taskdef>
</project>

in one of our build.xml we have following code

<project name="Build" basedir=".">
    <import file="common.xml" />

    <ac:if>
        <ac:not>
            <isset property="android.available" />
        </ac:not>
        <ac:then>
            <echo message="android is not available" />
        </ac:then>
    </ac:if>
</project>

Now we assumed that as the namespace is defined in common.xml the same will get imported in our build.xml. But this is not happening. Instead i need to include xmlns:ac="antlib:net.sf.antcontrib" eacch for my build.xml. i.e my build.xml should be like below

<project name="Build" basedir="." xmlns:ac="antlib:net.sf.antcontrib">
    <import file="common.xml" />

    <ac:if>
        <ac:not>
            <isset property="android.available" />
        </ac:not>
        <ac:then>
            <echo message="android is not available" />
        </ac:then>
    </ac:if>
</project>

Is there a way to do this namespace import instead in all our build.xml instead of defining the same namespace definition in all our build.xml

1
No. The XML namespace declaration in an XML thing. For example what if you imported two files each providing the "ac" prefix. Which one should the XML parser use? That is why the namespace declaration must appear at the top of your XML file.Mark O'Connor

1 Answers

0
votes

I have a different way to work with more then one ant file. My common ant file starts with following lines:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="nfa">
    <description>
        This file contains private task for:
        Test and Analyze th ......

I have to include my common ant file:

<include file="anttasks/private_ant_tasks.xml"/>

And in my main ant file I can use this private tasks like this:

<antcall target="nfa.analyze" />

In this way I have also well organized ant tasks and files.