Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

ant

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

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

I'm using Eclipse 3.4.2.

Please let me know what I'm missing here.

like image 360
TestUser Avatar asked Aug 27 '10 04:08

TestUser


People also ask

Which version of Java does ant use?

The later the version of Java, the more Ant tasks you get. The git branch 1.9. x is used for long term support of Ant 1.9. x versions that can be built and run with Java 1.5.

What is the default target in ant?

the default target to use when no target is supplied. No; however, since Ant 1.6. 0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.

How does Apache ant work?

Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets. A target can be directly invoked via Ant.

What is target in build XML?

An Ant target is a sequence of tasks to be executed to perform a part (or whole) of the build process. Ant targets are defined by the user of Ant. Thus, what tasks an Ant target contains depends on what the user of Ant is trying to do in the build script.


2 Answers

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

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
like image 85
martin clayton Avatar answered Oct 04 '22 23:10

martin clayton


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

However, the error message you get shows this namespace 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>
like image 39
nemo Avatar answered Oct 04 '22 23:10

nemo