apache/maven · error · IllegalStateException
Cannot add two different pieces of metadata for: " + getKey(
Error message
Cannot add two different pieces of metadata for: " + getKey()
What it means
Thrown by ProjectArtifactMetadata.merge() when two metadata objects with the same key ('project groupId:artifactId') carry different pom files. Legacy resolution merges ArtifactMetadata instances per artifact; if the same project metadata is added twice with distinct File objects, that invariant is violated and an IllegalStateException reports the key. This is a programming/config error in the code assembling metadata, not an environment issue.
Source
Thrown at compat/maven-compat/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java:111
public boolean storedInArtifactVersionDirectory() {
return true;
}
@Override
public String getBaseVersion() {
return artifact.getBaseVersion();
}
@Override
public Object getKey() {
return "project " + artifact.getGroupId() + ":" + artifact.getArtifactId();
}
@Override
public void merge(ArtifactMetadata metadata) {
ProjectArtifactMetadata m = (ProjectArtifactMetadata) metadata;
if (!m.file.equals(file)) {
throw new IllegalStateException("Cannot add two different pieces of metadata for: " + getKey());
}
}
@Override
public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
this.merge((ArtifactMetadata) metadata);
}
}
View on GitHub (pinned to e4093d4e12)
Solutions
- Find where two different pom files are attached to the same artifact (log artifact.getMetadataList() before merge) and keep only one
- When constructing ProjectArtifactMetadata yourself, reuse the same File instance for the same artifact
- If two artifacts genuinely need different poms, give them distinct artifactIds (or classifiers via proper attach APIs) instead of merging metadata
- Upgrade plugins that manipulate POM metadata manually - prefer MavenProjectHelper.attachArtifact / standard install flows
Example fix
// before artifact.addMetadata(new ProjectArtifactMetadata(artifact, pomA)); artifact.addMetadata(new ProjectArtifactMetadata(artifact, pomB)); // boom // after artifact.addMetadata(new ProjectArtifactMetadata(artifact, pomA)); // attach pomB to a separate artifact with its own GAV/classifier
Defensive patterns
Strategy: validation
Validate before calling
// Before adding POM metadata, ensure no conflicting instance exists
boolean conflict = artifact.getMetadataList().stream()
.filter(ProjectArtifactMetadata.class::isInstance)
.map(ProjectArtifactMetadata.class::cast)
.anyMatch(m -> !m.getFile().equals(newPomFile));
if (conflict) throw new IllegalArgumentException("pom already attached for " + artifact.getId()); Prevention
- Attach exactly one POM per artifact GAV
- Reuse the same File instance when re-adding metadata
- Use standard install/attach APIs instead of hand-built ProjectArtifactMetadata
When it happens
Trigger: Calling artifact.getMetadataList() flows that add two ProjectArtifactMetadata instances with different `file` values for the same groupId:artifactId - e.g. attaching metadata for both a freshly generated pom.xml and an attached alternate pom file, or constructing the metadata twice with different paths.
Common situations: Custom build extensions or plugins that add POM metadata manually; attaching two POMs to one artifact (classifier confusion); test code building fake artifacts reusing the same GAV with different temp files; rarely, mixed legacy/core metadata merging during Maven 3->4 compat paths.
Related errors
- Cannot read metadata from '{}': {}
- {} could not be retrieved from repository: {} due to an erro
- Error installing metadata: {}
- Error while deploying metadata: {}
- Unable to get dependency information: " + e.getMessage()
AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21).
Data as JSON: /api/errors/c1dee2f8cc282f51.
Report an issue: GitHub.