apache/maven · error · IllegalArgumentException

Cannot attach artifact to project: groupId, artifactId and v

Error message

Cannot attach artifact to project: groupId, artifactId and version must match the project.%n  Project coordinates:  %s:%s:%s%n  Artifact coordinates: %s:%s:%s

What it means

IllegalArgumentException from ProjectManager.attachArtifact(project, artifact, path): the artifact's coordinates must match the project's. groupId and base version must equal the project's; artifactId must equal the project's artifactId — or, for modular multi-module source layouts, one of the module names declared in the project's source roots (sr.module()). The non-modular message variant prints both coordinate triples for comparison.

Source

Thrown at impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectManager.java:170

                                + "and artifactId must match either the project or a declared module name.%n"
                                + "  Project coordinates:  %s:%s:%s%n"
                                + "  Artifact coordinates: %s:%s:%s%n",
                        g1, a1, v1, g2, a2, v2);
                if (isSameGroupAndVersion) {
                    message += String.format(
                            "  Hint: The artifactId '%s' does not match the project artifactId '%s' "
                                    + "nor any declared module name in source roots.",
                            a2, a1);
                }
            } else {
                // Non-modular project: artifactId must match exactly
                message = String.format(
                        "Cannot attach artifact to project: groupId, artifactId and version must match the project.%n"
                                + "  Project coordinates:  %s:%s:%s%n"
                                + "  Artifact coordinates: %s:%s:%s",
                        g1, a1, v1, g2, a2, v2);
            }
            throw new IllegalArgumentException(message);
        }
        getMavenProject(project)
                .addAttachedArtifact(
                        RepositoryUtils.toArtifact(getSession(project).toArtifact(artifact)));
        artifactManager.setPath(artifact, path);
    }

    @Nonnull
    @Override
    public Collection<SourceRoot> getSourceRoots(@Nonnull Project project) {
        MavenProject prj = getMavenProject(requireNonNull(project, "project" + " cannot be null"));
        return prj.getSourceRoots();
    }

    @Nonnull
    @Override
    public Stream<SourceRoot> getEnabledSourceRoots(@Nonnull Project project, ProjectScope scope, Language language) {
        MavenProject prj = getMavenProject(requireNonNull(project, "project" + " cannot be null"));

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Build the attached artifact from the project's own groupId/artifactId/version and vary only classifier and extension
  2. For JPMS multi-module layouts, make the artifactId match a module name declared in a source root
  3. Validate coordinates against project.getGroupId()/getArtifactId()/getVersion() before attaching

Example fix

// before: coordinates differ from the project
Artifact a = session.createArtifact("other.group", "other-artifact", "1.0", "jar");
projectManager.attachArtifact(project, session.createProducedArtifact(
    "other.group", "other-artifact", "1.0", null, "jar", null), path);

// after: project coordinates, only classifier differs
ProducedArtifact a = session.createProducedArtifact(
    project.getGroupId(), project.getArtifactId(), project.getVersion(),
    "sources", "jar", null);
projectManager.attachArtifact(project, a, path);
Defensive patterns

Strategy: validation

Validate before calling

// Verify coordinates before attaching
boolean artifactIdOk = artifact.getArtifactId().equals(project.getArtifactId())
    || project.getSourceRoots().stream().map(SourceRoot::module)
        .flatMap(Optional::stream).anyMatch(artifact.getArtifactId()::equals);
if (!(artifact.getGroupId().equals(project.getGroupId())
        && artifact.getBaseVersion().toString().equals(project.getVersion())
        && artifactIdOk)) {
    throw new IllegalArgumentException(
        "Refusing to attach artifact with foreign coordinates: " + artifact);
}

Prevention

When it happens

Trigger: session.getService(ProjectManager.class).attachArtifact(...) with an artifact whose groupId or version differs from the project, or whose artifactId differs on a project with no module-declaring source roots. Empty coordinates are auto-filled from the project first, so only mismatching non-empty values reach the check.

Common situations: Mojos attaching secondary artifacts with hand-built coordinates instead of the project's; attaching an artifact produced for a different subproject; refactoring project coordinates without updating attachArtifact call sites; JPMS module names that differ from the project artifactId.

Related errors


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