【问题标题】:Importing namespace from one ant xml to another ant xml将命名空间从一个 ant xml 导入到另一个 ant xml
【发布时间】:2012-07-16 13:39:48
【问题描述】:

在我们的项目中,我们有一个 common.xml,它包含在我们所有的 ant 文件中。这个 common.xml 包含我们在项目中使用的所有常见任务。

最近我们想在我们的项目中使用 ant-contrib。所以我们在 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>

在我们的 build.xml 之一中,我们有以下代码

<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>

现在我们假设命名空间是在 common.xml 中定义的,同样的也会被导入到我们的 build.xml 中。但这并没有发生。相反,我需要为我的 build.xml 包含 xmlns:ac="antlib:net.sf.antcontrib" 每个。即我的 build.xml 应该如下所示

<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>

有没有办法在我们所有的 build.xml 中执行此命名空间导入,而不是在所有 build.xml 中定义相同的命名空间定义

【问题讨论】:

  • 没有。 XML 事物中的 XML 命名空间声明。例如,如果您导入了两个文件,每个文件都提供“ac”前缀。 XML 解析器应该使用哪一个?这就是命名空间声明必须出现在 XML 文件顶部的原因。

标签: ant build build-automation


【解决方案1】:

我有一种不同的方式来处理多个 ant 文件。我常用的 ant 文件以以下几行开头:

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

我必须包含我的常用 ant 文件:

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

在我的主 ant 文件中,我可以像这样使用这个 private 任务:

<antcall target="nfa.analyze" />

通过这种方式,我还很好地组织了 ant 任务和文件。

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多