{"record":{"id":"c1dee2f8cc282f51","repo":"apache/maven","slug":"cannot-add-two-different-pieces-of-metadata-for","errorCode":null,"errorMessage":"Cannot add two different pieces of metadata for: \" + getKey()","messagePattern":"Cannot add two different pieces of metadata for: \" \\+ getKey\\(\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"compat/maven-compat/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java","lineNumber":111,"sourceCode":"    public boolean storedInArtifactVersionDirectory() {\n        return true;\n    }\n\n    @Override\n    public String getBaseVersion() {\n        return artifact.getBaseVersion();\n    }\n\n    @Override\n    public Object getKey() {\n        return \"project \" + artifact.getGroupId() + \":\" + artifact.getArtifactId();\n    }\n\n    @Override\n    public void merge(ArtifactMetadata metadata) {\n        ProjectArtifactMetadata m = (ProjectArtifactMetadata) metadata;\n        if (!m.file.equals(file)) {\n            throw new IllegalStateException(\"Cannot add two different pieces of metadata for: \" + getKey());\n        }\n    }\n\n    @Override\n    public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {\n        this.merge((ArtifactMetadata) metadata);\n    }\n}\n","sourceCodeStart":93,"sourceCodeEnd":120,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/compat/maven-compat/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java#L93-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nartifact.addMetadata(new ProjectArtifactMetadata(artifact, pomA));\nartifact.addMetadata(new ProjectArtifactMetadata(artifact, pomB)); // boom\n// after\nartifact.addMetadata(new ProjectArtifactMetadata(artifact, pomA));\n// attach pomB to a separate artifact with its own GAV/classifier","handlingStrategy":"validation","validationCode":"// Before adding POM metadata, ensure no conflicting instance exists\nboolean conflict = artifact.getMetadataList().stream()\n    .filter(ProjectArtifactMetadata.class::isInstance)\n    .map(ProjectArtifactMetadata.class::cast)\n    .anyMatch(m -> !m.getFile().equals(newPomFile));\nif (conflict) throw new IllegalArgumentException(\"pom already attached for \" + artifact.getId());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["maven","metadata","illegal-state","artifact","merge"],"backgroundTag":"duplicate-metadata-conflict","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}