apache/maven · error · UnsupportedOperationException

Cannot change the scoping information for an attached artifa

Error message

Cannot change the scoping information for an attached artifact. It is derived from the main artifact.

What it means

Scope is a dependency attribute inherited from the main artifact: getScope() delegates to the parent, and setScope throws UnsupportedOperationException. Attached artifacts are not dependency declarations, so their scope cannot be edited.

Source

Thrown at impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java:136

    @Override
    public ArtifactRepository getRepository() {
        return parent.getRepository();
    }

    @Override
    public void setRepository(ArtifactRepository repository) {
        throw new UnsupportedOperationException("Cannot change the repository information for an attached artifact."
                + " It is derived from the main artifact.");
    }

    @Override
    public String getScope() {
        return parent.getScope();
    }

    @Override
    public void setScope(String scope) {
        throw new UnsupportedOperationException("Cannot change the scoping information for an attached artifact."
                + " It is derived from the main artifact.");
    }

    @Override
    public String getVersion() {
        return parent.getVersion();
    }

    @Override
    public void setVersion(String version) {
        throw new UnsupportedOperationException("Cannot change the version information for an attached artifact."
                + " It is derived from the main artifact.");
    }

    @Override
    public VersionRange getVersionRange() {
        return parent.getVersionRange();
    }

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Adjust scope on the Dependency declaration or the main artifact instead: attached artifacts have no independent scope
  2. Configure scope changes via dependency declarations or plugin configuration
  3. Skip attached artifacts in scope processing

Example fix

// before
attached.setScope(Artifact.SCOPE_RUNTIME);

// after
// scope belongs to dependencies, not attached artifacts
dependency.setScope(Artifact.SCOPE_RUNTIME);
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("scope is a dependency attribute; set it on the Dependency");
}

Prevention

When it happens

Trigger: Calling setScope(...) on an AttachedArtifact, e.g. classpath-composition code trying to widen or narrow scopes on every artifact of a project.

Common situations: Scope-manipulation logic that treats project.getAttachedArtifacts() like dependency artifacts; plugins reused from DefaultArtifact code paths.

Related errors


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