apache/maven · error · UnsupportedOperationException
Cannot change the artifactId for an attached artifact. It is
Error message
Cannot change the artifactId for an attached artifact. It is derived from the main artifact.
What it means
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.
Source
Thrown at impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java:75
this.parent = parent;
if (getId().equals(parent.getId())) {
throw new InvalidArtifactRTException(
parent.getGroupId(),
parent.getArtifactId(),
parent.getVersion(),
parent.getType(),
"An attached artifact must have a different ID" + " than its corresponding main artifact.");
}
}
public AttachedArtifact(Artifact parent, String type, ArtifactHandler artifactHandler) {
this(parent, type, null, artifactHandler);
}
@Override
public void setArtifactId(String artifactId) {
throw new UnsupportedOperationException(
"Cannot change the artifactId for an attached artifact." + " It is derived from the main artifact.");
}
@Override
public List<ArtifactVersion> getAvailableVersions() {
return parent.getAvailableVersions();
}
@Override
public void setAvailableVersions(List<ArtifactVersion> availableVersions) {
throw new UnsupportedOperationException("Cannot change the version information for an attached artifact."
+ " It is derived from the main artifact.");
}
@Override
public String getBaseVersion() {
return parent.getBaseVersion();
}View on GitHub (pinned to e4093d4e12)
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
Example fix
// before
attached.setArtifactId("new-id"); // UnsupportedOperationException
// after
Artifact standalone = new DefaultArtifact(groupId, "new-id", version, scope, type, classifier, handler); Defensive patterns
Strategy: type-guard
Type guard
static boolean isAttachedArtifact(Artifact a) {
return a instanceof org.apache.maven.project.artifact.AttachedArtifact;
} Try / catch
if (isAttachedArtifact(artifact)) {
throw new IllegalStateException("artifactId is derived from the main artifact; change the project instead");
} Prevention
- Treat artifacts from getAttachedArtifacts() as read-only views over the main artifact
- Use MavenProjectHelper.attachArtifact to add artifacts instead of mutating existing attached ones
When it happens
Trigger: Calling setArtifactId(...) on an artifact obtained from MavenProject.getAttachedArtifacts() or created by MavenProjectHelper.attachArtifact, both of which yield AttachedArtifact instances.
Common situations: 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.
Related errors
- Cannot change the version information for an attached artifa
- Cannot change the download information for an attached artif
- Cannot change the groupId for an attached artifact. It is de
- Cannot change the repository information for an attached art
- Cannot change the scoping information for an attached artifa
AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21).
Data as JSON: /api/errors/f84fd8841f319339.
Report an issue: GitHub.