gradle/gradle · error · IllegalStateException
Can not use getCredentials() method when not using PasswordC
Error message
Can not use getCredentials() method when not using PasswordCredentials; please use getCredentials(Class)
What it means
The parameterless getCredentials() on Maven/Ivy repositories is a convenience hard-wired to PasswordCredentials: on first call it installs PasswordCredentials, otherwise it returns them. If the repository already uses a different credentials type (e.g. AwsCredentials configured earlier), the instanceof check fails and IllegalStateException directs you to the typed variant getCredentials(Class).
Source
Thrown at platforms/software/dependency-management/src/main/java/org/gradle/api/internal/artifacts/repositories/AuthenticationSupporter.java:62
private final ProviderFactory providerFactory;
private final Property<Credentials> credentials;
private boolean usesCredentials = false;
public AuthenticationSupporter(Instantiator instantiator, ObjectFactory objectFactory, AuthenticationContainer authenticationContainer, ProviderFactory providerFactory) {
this.instantiator = instantiator;
this.authenticationContainer = authenticationContainer;
this.credentials = objectFactory.property(Credentials.class);
this.providerFactory = providerFactory;
}
public PasswordCredentials getCredentials() {
if (!usesCredentials()) {
return setCredentials(PasswordCredentials.class);
} else if (credentials.get() instanceof PasswordCredentials) {
return Cast.uncheckedCast(credentials.get());
} else {
throw new IllegalStateException("Can not use getCredentials() method when not using PasswordCredentials; please use getCredentials(Class)");
}
}
public <T extends Credentials> T getCredentials(Class<T> credentialsType) {
if (!usesCredentials()) {
return setCredentials(credentialsType);
} else if (credentialsType.isInstance(credentials.get())) {
return Cast.uncheckedCast(credentials.get());
} else {
throw new IllegalArgumentException(String.format("Given credentials type '%s' does not match actual type '%s'", credentialsType.getName(), getCredentialsPublicType(credentials.get().getClass()).getName()));
}
}
public void credentials(Action<? super PasswordCredentials> action) {
if (usesCredentials() && !(credentials.get() instanceof PasswordCredentials)) {
throw new IllegalStateException("Can not use credentials(Action) method when not using PasswordCredentials; please use credentials(Class, Action)");
}
credentials(PasswordCredentials.class, action);View on GitHub (pinned to 534f27719b)
Solutions
- Use the typed getter matching the configured type: repo.getCredentials(AwsCredentials.class).
- In generic code, inspect the current credentials via repo.getCredentials(Credentials.class) — the base type always matches — and branch with instanceof.
- If only username/password are needed everywhere, standardize repositories on PasswordCredentials.
Example fix
// before
repo.credentials(AwsCredentials.class) { accessKey = 'key'; secretKey = 'secret' }
def user = repo.credentials.username // implicit getCredentials() -> IllegalStateException
// after
def aws = repo.getCredentials(AwsCredentials.class)
println aws.accessKey Defensive patterns
Strategy: type-guard
Type guard
static boolean usesPasswordCredentials(MavenArtifactRepository repo) {
return repo.getCredentials(Credentials.class) instanceof PasswordCredentials
} Prevention
- In generic code, always fetch getCredentials(Credentials.class) and branch with instanceof
- Never assume the no-arg getCredentials() works on every repository
- Pick one credentials style per repository and stay consistent
When it happens
Trigger: repo.credentials(AwsCredentials.class) { ... } (or repo.getCredentials(AwsCredentials.class)) followed by repo.getCredentials() — for example generic code that introspects repository credentials on an S3 repository.
Common situations: Generic repository-auditing plugins calling the no-arg getter on every repository; builds mixing AWS and password credentials; migrations from password-only setups where the no-arg getter used to work.
Related errors
- Can not use credentials(Action) method when not using Passwo
- Given credentials type '%s' does not match actual type '%s'
- Unknown credentials type: '%s' (supported types: %s, %s and
- Credentials must be an instance of '%s'.
- Cannot fetch a model of type '%s' as this type is not an int
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/083a6d7c29caea2e.
Report an issue: GitHub.