apache/maven · error · IllegalArgumentException

Unsupported Artifact class:

Error message

Unsupported Artifact class: 

What it means

AbstractSession.getArtifact(Class, artifact) is a closed dispatch: it only accepts exactly Artifact.class, DownloadedArtifact.class or ProducedArtifact.class. Any other Class object (including subclasses of these three, or unrelated classes) falls through the if-chain and throws IllegalArgumentException. This is an API-misuse guard for the Maven 4 consumer API's artifact cache.

Source

Thrown at impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java:270

    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends Artifact> T getArtifact(Class<T> clazz, org.eclipse.aether.artifact.Artifact artifact) {
        Cache<org.eclipse.aether.artifact.Artifact, Artifact> map = allArtifacts.computeIfAbsent(
                clazz, c -> Cache.newCache(Cache.ReferenceType.WEAK, "AbstractSession-Artifacts-" + c.getSimpleName()));
        if (clazz == Artifact.class) {
            return (T) map.computeIfAbsent(artifact, a -> new DefaultArtifact(this, a));
        } else if (clazz == DownloadedArtifact.class) {
            if (artifact.getPath() == null) {
                throw new IllegalArgumentException("The given artifact is not resolved");
            } else {
                return (T) map.computeIfAbsent(artifact, a -> new DefaultDownloadedArtifact(this, a));
            }
        } else if (clazz == ProducedArtifact.class) {
            return (T) map.computeIfAbsent(artifact, a -> new DefaultProducedArtifact(this, a));
        } else {
            throw new IllegalArgumentException("Unsupported Artifact class: " + clazz);
        }
    }

    @Nonnull
    @Override
    public Dependency getDependency(@Nonnull org.eclipse.aether.graph.Dependency dependency) {
        return allDependencies.computeIfAbsent(dependency, d -> new DefaultDependency(this, d));
    }

    @Override
    public List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories) {
        return repositories == null ? null : map(repositories, this::toRepository);
    }

    @Override
    public List<org.eclipse.aether.repository.RemoteRepository> toResolvingRepositories(
            List<RemoteRepository> repositories) {
        return getRepositorySystem().newResolutionRepositories(getSession(), toRepositories(repositories));

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Pass one of the exact literals: Artifact.class, DownloadedArtifact.class or ProducedArtifact.class
  2. If you hold a Class<? extends Artifact> from generic code, switch on it explicitly and reject unknown values early with your own error message
  3. Do not subclass the API artifact types expecting getArtifact to instantiate them

Example fix

// before
<T extends Artifact> T get(Session s, Class<T> clazz, org.eclipse.aether.artifact.Artifact a) {
    return s.getArtifact(clazz, a); // throws for subclasses
}

// after
<T extends Artifact> T get(Session s, Class<T> clazz, org.eclipse.aether.artifact.Artifact a) {
    if (clazz != Artifact.class && clazz != DownloadedArtifact.class && clazz != ProducedArtifact.class) {
        throw new IllegalArgumentException('Use Artifact, DownloadedArtifact or ProducedArtifact, got ' + clazz);
    }
    return s.getArtifact(clazz, a);
}
Defensive patterns

Strategy: type-guard

Validate before calling

private static boolean isSupportedArtifactClass(Class<?> clazz) {
    return clazz == Artifact.class || clazz == DownloadedArtifact.class || clazz == ProducedArtifact.class;
}

Type guard

static boolean isSupportedArtifactClass(Class<? extends Artifact> clazz) {
    return clazz == Artifact.class || clazz == DownloadedArtifact.class || clazz == ProducedArtifact.class;
}

Prevention

When it happens

Trigger: Calling session.getArtifact(SomeSubclassOfArtifact.class, artifact), session.getArtifact(null, artifact) with a non-null check bypassed, or passing e.g. ArtifactCoordinates.class / a custom Artifact implementation class as the first argument.

Common situations: Writing generic utility code that passes a Class<? extends Artifact> variable captured from generics, so the compiler cannot force one of the three supported literals; migrating from an older internal API that accepted arbitrary artifact types.

Related errors


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