[MNG-7787] Introduce new options for plugin validation report (#1113)

Added NONE and INLINE modes.

---

https://issues.apache.org/jira/browse/MNG-7787
This commit is contained in:
Tamas Cservenak 2023-05-19 17:47:03 +02:00
parent a90950155c
commit 11d97e64e7
2 changed files with 33 additions and 15 deletions

View File

@ -571,7 +571,7 @@ public <T> T getConfiguredMojo(Class<T> mojoInterface, MavenSession session, Moj
session,
mojoDescriptor,
mojo.getClass(),
"Implements `Contextualizable` interface from Plexus Container, which is EOL.");
"Mojo implements `Contextualizable` interface from Plexus Container, which is EOL.");
}
for (MavenPluginDependenciesValidator validator : dependenciesValidators) {

View File

@ -51,10 +51,12 @@ public final class DefaultPluginValidationManager extends AbstractMavenLifecycle
private static final String MAVEN_PLUGIN_VALIDATION_KEY = "maven.plugin.validation";
private enum ValidationLevel {
BRIEF,
DEFAULT,
VERBOSE
private enum ValidationReportLevel {
NONE, // mute validation completely (validation issue collection still happens, it is just not reported!)
INLINE, // inline, each problem one line next to mojo invocation, repeated as many times as mojo is executed
BRIEF, // at end, one line with count of plugins in the build having validation issues
DEFAULT, // at end, list of plugin GAVs in the build having validation issues
VERBOSE // at end, detailed report of plugins in the build having validation issues
}
private final Logger logger = LoggerFactory.getLogger(getClass());
@ -64,20 +66,20 @@ public void afterSessionEnd(MavenSession session) {
reportSessionCollectedValidationIssues(session);
}
private ValidationLevel validationLevel(RepositorySystemSession session) {
private ValidationReportLevel validationReportLevel(RepositorySystemSession session) {
String level = ConfigUtils.getString(session, null, MAVEN_PLUGIN_VALIDATION_KEY);
if (level == null || level.isEmpty()) {
return ValidationLevel.DEFAULT;
return ValidationReportLevel.DEFAULT;
}
try {
return ValidationLevel.valueOf(level.toUpperCase(Locale.ENGLISH));
return ValidationReportLevel.valueOf(level.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
logger.warn(
"Invalid value specified for property {}: '{}'. Supported values are (case insensitive): {}",
MAVEN_PLUGIN_VALIDATION_KEY,
level,
Arrays.toString(ValidationLevel.values()));
return ValidationLevel.DEFAULT;
Arrays.toString(ValidationReportLevel.values()));
return ValidationReportLevel.DEFAULT;
}
}
@ -100,6 +102,10 @@ public void reportPluginValidationIssue(RepositorySystemSession session, Artifac
PluginValidationIssues pluginIssues =
pluginIssues(session).computeIfAbsent(pluginKey, k -> new PluginValidationIssues());
pluginIssues.reportPluginIssue(null, null, issue);
ValidationReportLevel validationReportLevel = validationReportLevel(session);
if (validationReportLevel == ValidationReportLevel.INLINE) {
logger.warn(" {}", issue);
}
}
@Override
@ -109,6 +115,10 @@ public void reportPluginValidationIssue(MavenSession mavenSession, MojoDescripto
.computeIfAbsent(pluginKey, k -> new PluginValidationIssues());
pluginIssues.reportPluginIssue(
pluginDeclaration(mavenSession, mojoDescriptor), pluginOccurrence(mavenSession), issue);
ValidationReportLevel validationReportLevel = validationReportLevel(mavenSession.getRepositorySession());
if (validationReportLevel == ValidationReportLevel.INLINE) {
logger.warn(" {}", issue);
}
}
@Override
@ -122,26 +132,34 @@ public void reportPluginMojoValidationIssue(
pluginOccurrence(mavenSession),
mojoInfo(mojoDescriptor, mojoClass),
issue);
ValidationReportLevel validationReportLevel = validationReportLevel(mavenSession.getRepositorySession());
if (validationReportLevel == ValidationReportLevel.INLINE) {
logger.warn(" {}", issue);
}
}
private void reportSessionCollectedValidationIssues(MavenSession mavenSession) {
if (!logger.isWarnEnabled()) {
return; // nothing can be reported
}
ValidationLevel validationLevel = validationLevel(mavenSession.getRepositorySession());
ValidationReportLevel validationReportLevel = validationReportLevel(mavenSession.getRepositorySession());
if (validationReportLevel == ValidationReportLevel.NONE
|| validationReportLevel == ValidationReportLevel.INLINE) {
return; // we were asked to not report anything OR reporting already happened inline
}
ConcurrentHashMap<String, PluginValidationIssues> issuesMap = pluginIssues(mavenSession.getRepositorySession());
if (!issuesMap.isEmpty()) {
logger.warn("");
logger.warn("Plugin validation issues were detected in {} plugin(s)", issuesMap.size());
logger.warn("");
if (validationLevel == ValidationLevel.BRIEF) {
if (validationReportLevel == ValidationReportLevel.BRIEF) {
return;
}
for (Map.Entry<String, PluginValidationIssues> entry : issuesMap.entrySet()) {
logger.warn(" * {}", entry.getKey());
if (validationLevel == ValidationLevel.VERBOSE) {
if (validationReportLevel == ValidationReportLevel.VERBOSE) {
PluginValidationIssues issues = entry.getValue();
if (!issues.pluginDeclarations.isEmpty()) {
logger.warn(" Declared at location(s):");
@ -174,13 +192,13 @@ private void reportSessionCollectedValidationIssues(MavenSession mavenSession) {
}
}
logger.warn("");
if (validationLevel == ValidationLevel.VERBOSE) {
if (validationReportLevel == ValidationReportLevel.VERBOSE) {
logger.warn(
"Fix reported issues by adjusting plugin configuration or by upgrading above listed plugins. If no upgrade available, please notify plugin maintainers about reported issues.");
}
logger.warn(
"For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): {}",
Arrays.toString(ValidationLevel.values()));
Arrays.toString(ValidationReportLevel.values()));
logger.warn("");
}
}