Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,10 @@ private void validateEffectiveDependencies(
prefix, "version", problems, errOn30, Version.V20, d.getVersion(), d.getManagementKey(), d);

/*
* TODO Extensions like Flex Mojos use custom scopes like "merged", "internal", "external", etc. In
* order to don't break backward-compat with those, only warn but don't error out.
* Extensions like Flex Mojos use custom scopes like "merged", "internal", "external", etc. In
* order to not break backward-compat with those, only warn but don't error out.
*/
validateEnum(
validateDependencyScope(
prefix,
"scope",
problems,
Expand All @@ -797,15 +797,11 @@ private void validateEffectiveDependencies(
d.getScope(),
d.getManagementKey(),
d,
"provided",
"compile",
"runtime",
"test",
"system");
false);

validateEffectiveModelAgainstDependency(prefix, problems, m, d, request);
} else {
validateEnum(
validateDependencyScope(
prefix,
"scope",
problems,
Expand All @@ -814,12 +810,7 @@ private void validateEffectiveDependencies(
d.getScope(),
d.getManagementKey(),
d,
"provided",
"compile",
"runtime",
"test",
"system",
"import");
true);
}
}
}
Expand Down Expand Up @@ -1462,6 +1453,58 @@ private boolean validateEnum(
return false;
}

@SuppressWarnings("checkstyle:parameternumber")
private boolean validateDependencyScope(
String prefix,
String fieldName,
ModelProblemCollector problems,
Severity severity,
Version version,
String scope,
String sourceHint,
InputLocationTracker tracker,
boolean isDependencyManagement) {
if (scope == null || scope.length() <= 0) {
return true;
}

String[] validScopes;
if (isDependencyManagement) {
validScopes = new String[] {"provided", "compile", "runtime", "test", "system", "import"};
} else {
validScopes = new String[] {"provided", "compile", "runtime", "test", "system"};
}

List<String> values = Arrays.asList(validScopes);

if (values.contains(scope)) {
return true;
}

// Provide a more helpful error message for the 'import' scope
if ("import".equals(scope) && !isDependencyManagement) {
addViolation(
problems,
severity,
version,
prefix + fieldName,
sourceHint,
"has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.",
tracker);
} else {
addViolation(
problems,
severity,
version,
prefix + fieldName,
sourceHint,
"must be one of " + values + " but is '" + scope + "'.",
tracker);
}

return false;
}

@SuppressWarnings("checkstyle:parameternumber")
private boolean validateModelVersion(
ModelProblemCollector problems, String string, InputLocationTracker tracker, String... validVersions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ void testBadDependencyScope() throws Exception {
assertViolations(result, 0, 0, 2);

assertTrue(result.getWarnings().get(0).contains("test:f"));
// Check that the import scope error message is more helpful
assertTrue(result.getWarnings()
.get(0)
.contains("has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections"));

assertTrue(result.getWarnings().get(1).contains("test:g"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,12 +1220,12 @@ private void validateEffectiveDependencies(
dependency);

/*
* TODO Extensions like Flex Mojos use custom scopes like "merged", "internal", "external", etc. In
* order to don't break backward-compat with those, only warn but don't error out.
* Extensions like Flex Mojos use custom scopes like "merged", "internal", "external", etc. In
* order to not break backward-compat with those, only warn but don't error out.
*/
ScopeManager scopeManager =
InternalSession.from(session).getSession().getScopeManager();
validateEnum(
validateDependencyScope(
prefix,
"scope",
problems,
Expand All @@ -1237,7 +1237,8 @@ private void validateEffectiveDependencies(
scopeManager.getDependencyScopeUniverse().stream()
.map(DependencyScope::getId)
.distinct()
.toArray(String[]::new));
.toArray(String[]::new),
false);

validateEffectiveModelAgainstDependency(prefix, problems, model, dependency);
} else {
Expand All @@ -1247,7 +1248,7 @@ private void validateEffectiveDependencies(
.map(DependencyScope::getId)
.collect(Collectors.toCollection(HashSet::new));
scopes.add("import");
validateEnum(
validateDependencyScope(
prefix,
"scope",
problems,
Expand All @@ -1256,7 +1257,8 @@ private void validateEffectiveDependencies(
dependency.getScope(),
SourceHint.dependencyManagementKey(dependency),
dependency,
scopes.toArray(new String[0]));
scopes.toArray(new String[0]),
true);
}
}
}
Expand Down Expand Up @@ -1993,6 +1995,52 @@ private boolean validateEnum(
return false;
}

@SuppressWarnings("checkstyle:parameternumber")
private boolean validateDependencyScope(
String prefix,
String fieldName,
ModelProblemCollector problems,
Severity severity,
Version version,
String scope,
@Nullable SourceHint sourceHint,
InputLocationTracker tracker,
String[] validScopes,
boolean isDependencyManagement) {
if (scope == null || scope.isEmpty()) {
return true;
}

List<String> values = Arrays.asList(validScopes);

if (values.contains(scope)) {
return true;
}

// Provide a more helpful error message for the 'import' scope
if ("import".equals(scope) && !isDependencyManagement) {
addViolation(
problems,
severity,
version,
prefix + fieldName,
sourceHint,
"has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.",
tracker);
} else {
addViolation(
problems,
severity,
version,
prefix + fieldName,
sourceHint,
"must be one of " + values + " but is '" + scope + "'.",
tracker);
}

return false;
}

@SuppressWarnings("checkstyle:parameternumber")
private boolean validateModelVersion(
Session session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ void testBadDependencyScope() throws Exception {
assertViolations(result, 0, 0, 2);

assertTrue(result.getWarnings().get(0).contains("groupId='test', artifactId='f'"));
// Check that the import scope error message is more helpful
assertTrue(result.getWarnings()
.get(0)
.contains("has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections"));

assertTrue(result.getWarnings().get(1).contains("groupId='test', artifactId='g'"));
}
Expand Down
Loading