java 如何修复 Ant 构建中的“意外元素“{}目标”{antlib:org.apache.tools.ant}目标”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3581251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:30:55  来源:igfitidea点击:

How do I fix 'Unexpected element "{}target" {antlib:org.apache.tools.ant}target' errors in my Ant build?

javaant

提问by TestUser

When I run my Ant build it fails with the following exception:

当我运行 Ant 构建时,它失败并出现以下异常:

Unexpected element "{}target" {antlib:org.apache.tools.ant}target

I'm using Eclipse 3.4.2.

我正在使用 Eclipse 3.4.2。

Please let me know what I'm missing here.

请让我知道我在这里缺少什么。

回答by martin clayton

I can reproduce this problem by putting a targetoutside the projectelement that is required in ant build files. The error indicates that there is a fault in your build file - something (a targetelement) in the wrong place.

我可以通过在 ant 构建文件中所需targetproject元素之外放置一个来重现这个问题。该错误表明您的构建文件中存在错误 - 某些(target元素)位于错误的位置。

This build throws that error:

此构建引发该错误:

<?xml version="1.0"?>
    <target name="wibble" />
<project name="stack_overflow">
</project>

Whereas this does not:

而这不会:

<?xml version="1.0"?>
<project name="stack_overflow">
    <target name="wibble" />
</project>

In the full error message you should get a line number to guide you to the point in the file needing attention:

在完整的错误消息中,您应该得到一个行号来引导您到文件中需要注意的地方:

#   line number here
#         v
build.xml:2: Unexpected element "{}target" {antlib:org.apache.tools.ant}target

回答by nemo

Surprinsingly, the apache foundation doesn't clearly express the namespace in which ant scripts are to be written. Yet it's an important XML concept...

令人惊讶的是,apache 基金会并没有明确表示要在其中编写 ant 脚本的命名空间。然而,它是一个重要的 XML 概念......

However, the error message you get shows this namespace URI: antlib:org.apache.tools.ant

但是,您收到的错误消息显示了这个命名空间 URI:antlib:org.apache.tools.ant

Although most of the time you don't need to bound the default namespace, this may help in your case:

尽管大多数情况下您不需要绑定默认命名空间,但这可能对您有帮助:

<project xmlns='antlib:org.apache.tools.ant'>
  <!-- ... -->
</project>