apache/maven · error · XMLStreamException

Duplicated tag: '{}'

Error message

Duplicated tag: '{}'

What it means

checkDuplicate(tagName, parser, parsed) tracks recognized child element names of each parsed class in a Set. In models that declare flat mappings, the flat tags pass through a switch untouched; every other tag reaches the default branch, which throws when parsed.add(tagName) returns false - that child element already occurred under the same parent. It enforces single-occurrence (non-list) fields in both strict and lenient mode.

Source

Thrown at src/mdo/reader-stax.vm:570

#end
#if ( ! ${aliases.isEmpty()} )
        switch (tagName) {
  #foreach( $entry in $aliases.entrySet() )
        case "${entry.key}":
            tagName = "${entry.value}";
            break;
  #end
        }
#end
#if ( ! ${flats.isEmpty()} )
        switch (tagName) {
  #foreach( $entry in $flats.entrySet() )
        case "${entry.key}":
  #end
            break;
        default:
            if (!parsed.add(tagName)) {
                throw new XMLStreamException("Duplicated tag: '" + tagName + "'", parser.getLocation(), null);
            }
        }
#else
        if (!parsed.add(tagName)) {
            throw new XMLStreamException("Duplicated tag: '" + tagName + "'", parser.getLocation(), null);
        }
#end
        return tagName;
    }

    /**
     * Method checkUnknownAttribute.
     *
     * @param parser a parser object.
     * @param strict a strict object.
     * @param tagName a tagName object.
     * @param attribute a attribute object.
     * @throws XMLStreamException XMLStreamException if

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Use the exception's location (line/column) to find the second occurrence and delete one of the duplicates.
  2. Validate the document against its XSD first - maxOccurs=1 violations get clearer diagnostics.
  3. If repetition is genuinely needed, the model must define the field as a list - a model change and regeneration, not a runtime fix.

Example fix

<!-- before -->
<dependency>
  <groupId>org.apache.maven</groupId>
  <groupId>com.example</groupId>
  <artifactId>app</artifactId>
</dependency>

<!-- after -->
<dependency>
  <groupId>com.example</groupId>
  <artifactId>app</artifactId>
</dependency>
Defensive patterns

Strategy: try-catch

Validate before calling

// Schema-validate before the model reader: duplicates surface as maxOccurs violations
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Validator v = sf.newSchema(new StreamSource(pomXsdFile)).newValidator();
v.validate(new StreamSource(pomFile)); // throws SAXException with precise position

Try / catch

try {
    Model model = reader.read(in, true);
} catch (XMLStreamException e) {
    Location loc = e.getLocation();
    if (e.getMessage() != null && e.getMessage().startsWith("Duplicated tag")) {
        log.warn("duplicate element at line {} column {}", loc.getLineNumber(), loc.getColumnNumber());
    }
    throw e;
}

Prevention

When it happens

Trigger: Reading a document where a singular field's element repeats inside its parent: two <groupId> in one <dependency>, two <packaging> or two <description> under <project> - thrown even when strict=false.

Common situations: Copy-paste editing in pom.xml; merge conflicts resolved by keeping both blocks; generators emitting an element twice inside one parent.

Related errors


AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21). Data as JSON: /api/errors/c5276f7ee225e8a3. Report an issue: GitHub.