{"record":{"id":"f84fd8841f319339","repo":"apache/maven","slug":"cannot-change-the-artifactid-for-an-attached-artif","errorCode":null,"errorMessage":"Cannot change the artifactId for an attached artifact. It is derived from the main artifact.","messagePattern":"Cannot change the artifactId for an attached artifact\\. It is derived from the main artifact\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java","lineNumber":75,"sourceCode":"        this.parent = parent;\n\n        if (getId().equals(parent.getId())) {\n            throw new InvalidArtifactRTException(\n                    parent.getGroupId(),\n                    parent.getArtifactId(),\n                    parent.getVersion(),\n                    parent.getType(),\n                    \"An attached artifact must have a different ID\" + \" than its corresponding main artifact.\");\n        }\n    }\n\n    public AttachedArtifact(Artifact parent, String type, ArtifactHandler artifactHandler) {\n        this(parent, type, null, artifactHandler);\n    }\n\n    @Override\n    public void setArtifactId(String artifactId) {\n        throw new UnsupportedOperationException(\n                \"Cannot change the artifactId for an attached artifact.\" + \" It is derived from the main artifact.\");\n    }\n\n    @Override\n    public List<ArtifactVersion> getAvailableVersions() {\n        return parent.getAvailableVersions();\n    }\n\n    @Override\n    public void setAvailableVersions(List<ArtifactVersion> availableVersions) {\n        throw new UnsupportedOperationException(\"Cannot change the version information for an attached artifact.\"\n                + \" It is derived from the main artifact.\");\n    }\n\n    @Override\n    public String getBaseVersion() {\n        return parent.getBaseVersion();\n    }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java#L57-L93","documentation":"An AttachedArtifact shares the main artifact's groupId:artifactId:version; only type and classifier are its own. setArtifactId therefore always throws UnsupportedOperationException: the coordinate is derived from the parent and cannot be changed. The class is deprecated and internal; it exists to be created via attach flows, never mutated.","triggerScenarios":"Calling setArtifactId(...) on an artifact obtained from MavenProject.getAttachedArtifacts() or created by MavenProjectHelper.attachArtifact, both of which yield AttachedArtifact instances.","commonSituations":"Generic artifact-processing code (publishers, reconcilers, copy utilities) that calls setters on every Artifact it sees; refactors of code that previously handled the mutable DefaultArtifact.","solutions":["Adjust the artifactId at the source: set it on the project's main artifact before the attachment is created","If the artifact needs independent coordinates, create a standalone DefaultArtifact instead of an attached one","Guard mutators with an AttachedArtifact check before calling setters"],"exampleFix":"// before\nattached.setArtifactId(\"new-id\"); // UnsupportedOperationException\n\n// after\nArtifact standalone = new DefaultArtifact(groupId, \"new-id\", version, scope, type, classifier, handler);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isAttachedArtifact(Artifact a) {\n    return a instanceof org.apache.maven.project.artifact.AttachedArtifact;\n}","tryCatchPattern":"if (isAttachedArtifact(artifact)) {\n    throw new IllegalStateException(\"artifactId is derived from the main artifact; change the project instead\");\n}","preventionTips":["Treat artifacts from getAttachedArtifacts() as read-only views over the main artifact","Use MavenProjectHelper.attachArtifact to add artifacts instead of mutating existing attached ones"],"tags":["maven","artifact","attached-artifact","immutable","unsupported-operation"],"backgroundTag":"immutable-object-mutation","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}