apache/maven · error · InvalidArtifactRTException

For artifact {}: An attached artifact must have a different

Error message

For artifact {}: An attached artifact must have a different ID than its corresponding main artifact.

What it means

AttachedArtifact models a secondary artifact (sources, javadoc, tests, custom classifier) attached to a project's main artifact: it inherits groupId/artifactId/version from the parent and may differ only in type and classifier. The constructor compares both ids and throws InvalidArtifactRTException (a runtime exception) when they match, i.e. when type and classifier are identical to the main artifact's, because the attachment would collide with the artifact it is attached to.

Source

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

    private final Artifact parent;

    public AttachedArtifact(Artifact parent, String type, String classifier, ArtifactHandler artifactHandler) {
        super(
                parent.getGroupId(),
                parent.getArtifactId(),
                parent.getVersionRange(),
                parent.getScope(),
                type,
                classifier,
                artifactHandler,
                parent.isOptional());

        setDependencyTrail(Collections.singletonList(parent.getId()));

        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.");
    }

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Set a distinct classifier (sources, javadoc, tests, ...)
  2. Or attach with a different type (e.g. zip instead of the packaging type)
  3. Verify you are not re-attaching the main artifact itself

Example fix

// before
projectHelper.attachArtifact(project, "jar", null, extraFile); // jar project, type jar, no classifier

// after
projectHelper.attachArtifact(project, "jar", "extra", extraFile);
Defensive patterns

Strategy: validation

Validate before calling

boolean collidesWithMain = type.equals(main.getType()) && Objects.equals(classifier, main.getClassifier());
if (collidesWithMain) {
    throw new IllegalArgumentException("attached artifact needs a distinct type or classifier");
}
new AttachedArtifact(main, type, classifier, handler);

Try / catch

try {
    new AttachedArtifact(main, type, classifier, handler);
} catch (InvalidArtifactRTException e) {
    // message tells you the attached id equals the main id: pick another classifier or type
    log.warn("attach rejected for {}: choose a different classifier/type", main.getId());
}

Prevention

When it happens

Trigger: new AttachedArtifact(main, type, classifier, handler) where type equals main.getType() and classifier equals main's classifier (typically "jar"/null). The same rule surfaces through MavenProjectHelper.attachArtifact(project, type, classifier, file) with the same combination.

Common situations: A mojo attaching an extra jar without a classifier to a jar-packaged project; copy-pasted attachArtifact calls where only the file parameter changed; re-attaching the main artifact's own file.

Related errors


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