gradle/gradle · error · IllegalArgumentException

Cannot query artifacts for a project component (%s).

Error message

Cannot query artifacts for a project component (%s).

What it means

ArtifactResolutionQuery resolves artifacts from repositories, so it only accepts module components (ModuleComponentIdentifier: group:name:version from a repo). validateComponentIdentifier() explicitly rejects ProjectComponentIdentifier — components belonging to a project in the (composite) build — with this IllegalArgumentException, because a local project has no repository artifacts to query.

Source

Thrown at platforms/software/dependency-management/src/main/java/org/gradle/api/internal/artifacts/query/DefaultArtifactResolutionQuery.java:193

        for (ComponentIdentifier componentId : componentIds) {
            try {
                ComponentIdentifier validId = validateComponentIdentifier(componentId);
                componentResults.add(buildComponentResult(validId, componentMetaDataResolver, artifactResolver));
            } catch (Exception t) {
                componentResults.add(new DefaultUnresolvedComponentResult(componentId, t));
            }
        }

        return new DefaultArtifactResolutionResult(componentResults);
    }

    private ComponentIdentifier validateComponentIdentifier(ComponentIdentifier componentId) {
        if (componentId instanceof ModuleComponentIdentifier) {
            return componentId;
        }
        if (componentId instanceof ProjectComponentIdentifier) {
            throw new IllegalArgumentException(String.format("Cannot query artifacts for a project component (%s).", componentId.getDisplayName()));
        }

        throw new IllegalArgumentException(String.format("Cannot resolve the artifacts for component %s with unsupported type %s.", componentId.getDisplayName(), componentId.getClass().getName()));
    }

    private ComponentArtifactsResult buildComponentResult(ComponentIdentifier componentId, ComponentMetaDataResolver componentMetaDataResolver, ArtifactResolver artifactResolver) {
        BuildableComponentResolveResult moduleResolveResult = new DefaultBuildableComponentResolveResult();
        componentMetaDataResolver.resolve(componentId, DefaultComponentOverrideMetadata.EMPTY, moduleResolveResult);
        ComponentArtifactResolveMetadata component = moduleResolveResult.getState().prepareForArtifactResolution().getArtifactMetadata();
        DefaultComponentArtifactsResult componentResult = new DefaultComponentArtifactsResult(component.getId());
        for (Class<? extends Artifact> artifactType : artifactTypes) {
            addArtifacts(componentResult, artifactType, component, artifactResolver);
        }
        return componentResult;
    }

    private <T extends Artifact> void addArtifacts(
        DefaultComponentArtifactsResult artifacts,

View on GitHub (pinned to 534f27719b)

Solutions

  1. Filter to module components before querying: ids.findAll { it instanceof ModuleComponentIdentifier } (Groovy) or ids.filterIsInstance<ModuleComponentIdentifier>() (Kotlin).
  2. Handle project components separately — they are in the build, so read their artifacts via their own configurations/artifact views.
  3. If you expected a module coordinate, inspect why it resolved to a project: dependency substitution rules or includeBuild substitution are the usual causes.

Example fix

// before
def ids = configurations.runtimeClasspath.incoming.resolutionResult.allComponents*.id
def result = dependencies.createArtifactResolutionQuery()
        .forComponents(ids) // contains ProjectComponentIdentifier -> throws
        .withArtifacts(ComponentWithModule.class, MavenModuleArtifact.class).execute()
// after
def moduleIds = ids.findAll { it instanceof ModuleComponentIdentifier }
def result = dependencies.createArtifactResolutionQuery()
        .forComponents(moduleIds)
        .withArtifacts(ComponentWithModule.class, MavenModuleArtifact.class).execute()
Defensive patterns

Strategy: type-guard

Validate before calling

def moduleIds = ids.findAll { isQueryableComponent(it) }
def result = dependencies.createArtifactResolutionQuery()
        .forComponents(moduleIds)
        .withArtifacts(ComponentWithModule.class, MavenModuleArtifact.class).execute()

Type guard

static boolean isQueryableComponent(ComponentIdentifier id) {
    return id instanceof org.gradle.api.artifacts.component.ModuleComponentIdentifier
}

Prevention

When it happens

Trigger: Passing identifiers taken from a ResolutionResult straight into forComponents(...): resolution results mix external modules with local project components, and any project component id (from a subproject or an included-build substitution) triggers the error on execute().

Common situations: Tooling that iterates allComponents and queries artifact files (sources, licenses, module metadata); composite builds where dependencies substitute to included projects; dependency substitutions turning an external coordinate into a local project.

Related errors


AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22). Data as JSON: /api/errors/4cf0c353ae31df39. Report an issue: GitHub.