apache/maven · error · IllegalArgumentException
Invalid dependency scope:
Error message
Invalid dependency scope:
What it means
Session.requireDependencyScope(id) looks the id up in the DependencyScope enum and throws IllegalArgumentException for unknown ids. Maven 4 scopes are case-sensitive, hyphen-separated ids: none, '' (undefined), compile, compile-only, runtime, provided, test, test-only, test-runtime, system. Note the Maven 4 additions (compile-only, test-only, test-runtime) and that underscores or uppercase variants are NOT accepted.
Source
Thrown at impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java:1028
public Language requireLanguage(String id) {
return getService(LanguageRegistry.class).require(id);
}
@Override
public Packaging requirePackaging(String id) {
return getService(PackagingRegistry.class).require(id);
}
@Override
public ProjectScope requireProjectScope(String id) {
return getService(ProjectScopeRegistry.class).require(id);
}
@Override
public DependencyScope requireDependencyScope(String id) {
DependencyScope scope = DependencyScope.forId(requireNonNull(id, "id"));
if (scope == null) {
throw new IllegalArgumentException("Invalid dependency scope: " + id);
}
return scope;
}
@Override
public PathScope requirePathScope(String id) {
return getService(PathScopeRegistry.class).require(id);
}
@Override
public void setCurrentTrace(RequestTrace trace) {
getTraceHolder().set(trace);
}
@Override
public RequestTrace getCurrentTrace() {
return getTraceHolder().get();
}View on GitHub (pinned to e4093d4e12)
Solutions
- Use one of the exact enum ids: none, compile, compile-only, runtime, provided, test, test-only, test-runtime, system (or DependencyScope enum constants directly)
- Validate first with DependencyScope.forId(id) and report the supported list on null
- Prefer passing DependencyScope enum values instead of strings throughout your code
Example fix
// before
DependencyScope scope = session.requireDependencyScope(userScope); // throws for 'compile_only'
// after
DependencyScope scope = DependencyScope.forId(userScope);
if (scope == null) {
throw new IllegalArgumentException('Unknown scope "' + userScope + '"; valid: ' + Arrays.toString(DependencyScope.values()));
} Defensive patterns
Strategy: validation
Validate before calling
DependencyScope scope = DependencyScope.forId(scopeId);
if (scope == null) {
throw new IllegalArgumentException(
'Invalid scope "' + scopeId + '"; valid ids: ' + Arrays.toString(DependencyScope.values()));
} Type guard
static boolean isValidScope(String id) { return DependencyScope.forId(id) != null; } Prevention
- Use DependencyScope enum constants instead of strings in code
- Validate scope strings from user input against DependencyScope.forId before calling requireDependencyScope
- Remember ids are case-sensitive and hyphenated (compile-only, test-runtime)
When it happens
Trigger: Calling session.requireDependencyScope('TEST'), requireDependencyScope('compile_only'), or a legacy/typo string like 'compille' or Maven-3-style 'import'/'system' variants that are not in the enum.
Common situations: Porting Maven 3 code that manipulated scope strings; parsing scopes from user input or plugin parameters without validation; confusion between Maven 3 and Maven 4 scope vocabularies (e.g. expecting 'test-runtime' vs 'testruntime' vs 'TEST_RUNTIME'); 'import' scope which exists only in dependencyManagement and is not a DependencyScope id.
Related errors
- Unsupported Artifact class:
- Illegal request type: " + requestType
- Illegal event type: " + eventType
- Repository identifier missing
- URL missing for repository " + id
AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21).
Data as JSON: /api/errors/ed08e64e7e91ca43.
Report an issue: GitHub.