From 0fa660285a0e91bcbf2baa3883a99ad311b613d5 Mon Sep 17 00:00:00 2001 From: Swell <5782559+sultan@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:48:26 +0200 Subject: [PATCH] report verbosity --- .../mojo/versions/AbstractVersionsReport.java | 7 ++ .../versions/DependencyUpdatesReportMojo.java | 3 +- .../versions/ParentUpdatesReportMojo.java | 2 +- .../versions/PluginUpdatesReportMojo.java | 3 +- .../versions/PropertyUpdatesReportMojo.java | 2 +- .../versions/api/AbstractVersionDetails.java | 62 ++++++++++++++- .../AbstractVersionsReportRenderer.java | 76 ++++++------------- .../DependencyUpdatesReportRenderer.java | 11 +++ .../PluginUpdatesReportRenderer.java | 18 +++-- .../PropertyUpdatesReportRenderer.java | 11 +++ .../reporting/ReportRendererFactory.java | 5 +- .../reporting/ReportRendererFactoryImpl.java | 18 +++-- .../reporting/VersionsReportRendererBase.java | 22 ++++++ 13 files changed, 169 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java index 336febe831..5e203c19c7 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java @@ -143,6 +143,12 @@ public abstract class AbstractVersionsReport @Parameter( property = "allowSnapshots", defaultValue = "false" ) protected boolean allowSnapshots; + @Parameter( property = "verboseSummary", defaultValue = "true" ) + protected boolean verboseSummary = true; + + @Parameter( property = "verboseDetail", defaultValue = "true" ) + protected boolean verboseDetail = true; + /** * Our versions helper. */ @@ -293,6 +299,7 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver { final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories ); + artifactVersions.setVerboseDetail( verboseDetail ); return artifactVersions.getNewestVersion( versionRange, includeSnapshots ); } catch ( ArtifactMetadataRetrievalException e ) diff --git a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java index 6cda7845a4..b2799d2406 100644 --- a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java @@ -212,7 +212,8 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() ! new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ); if ( "html".equals( format ) ) { - rendererFactory.createReportRenderer( getOutputName(), sink, locale, model ).render(); + rendererFactory.createReportRenderer( getOutputName(), sink, locale, model, + verboseSummary, verboseDetail ).render(); } else if ( "xml".equals( format ) ) { diff --git a/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java index 54adc31619..9b02aa0521 100644 --- a/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/ParentUpdatesReportMojo.java @@ -112,7 +112,7 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx .withScope( artifactVersions.getArtifact().getScope() ) .withType( artifactVersions.getArtifact().getType() ) .withClassifier( artifactVersions.getArtifact().getClassifier() ) - .build(), artifactVersions ) ) + .build(), artifactVersions ), verboseSummary, verboseDetail ) .render(); } catch ( ArtifactMetadataRetrievalException e ) diff --git a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java index 1b47c0f7c4..53cccccaa9 100644 --- a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java @@ -174,7 +174,8 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx { if ( "html".equals( format ) ) { - rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, model ).render(); + rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, model, + verboseSummary, verboseDetail ).render(); } else if ( "xml".equals( format ) ) { diff --git a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java index 3299d0e0ed..195dd90cf7 100644 --- a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java @@ -128,7 +128,7 @@ protected void doGenerateReport( Locale locale, Sink sink ) throw new MavenReportException( e.getMessage(), e ); } rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, - new PropertyUpdatesModel( updateSet ) ).render(); + new PropertyUpdatesModel( updateSet ), verboseSummary, verboseDetail ).render(); } /** diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index fea66d453e..c885b73b17 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -21,11 +21,13 @@ import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.Optional; -import java.util.Set; import java.util.TreeSet; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; @@ -49,6 +51,10 @@ public abstract class AbstractVersionDetails implements VersionDetails { + private static final Pattern PREVIEW_PATTERN = + Pattern.compile( "(?i)(?:.*[-.](a|alpha|b|beta|m|mr|rm|preview|rc|cr)" + + "(\\d{0,2}[a-z]?|\\d{6}\\.\\d{4})|\\d{8}(?:\\.?\\d{6})?)$" ); + /** * The current version. Guarded by {@link #currentVersionLock}. * @@ -63,6 +69,8 @@ public abstract class AbstractVersionDetails */ private boolean includeSnapshots = false; + protected boolean verboseDetail = true; + /** * Not sure if we need to be thread safe, but there's no harm being careful, after all we could be invoked from an * IDE. @@ -123,6 +131,14 @@ public final void setIncludeSnapshots( boolean includeSnapshots ) } } + public void setVerboseDetail( boolean verboseDetail ) + { + synchronized ( currentVersionLock ) + { + this.verboseDetail = verboseDetail; + } + } + @Override public final ArtifactVersion[] getVersions() { @@ -285,7 +301,7 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, Restricti boolean includeSnapshots ) { final VersionComparator versionComparator = getVersionComparator(); - Set result = new TreeSet<>( versionComparator ); + TreeSet result = new TreeSet<>( versionComparator ); for ( ArtifactVersion candidate : getVersions( includeSnapshots ) ) { if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) ) @@ -302,6 +318,48 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, Restricti } result.add( candidate ); } + + // filter out intermediate minor versions. + if ( !verboseDetail ) + { + String current = "."; + boolean needOneMore = false; + Iterator rev = result.descendingIterator(); // be cautious to keep latest ones. + for ( Iterator it = rev; it.hasNext(); ) + { + ArtifactVersion version = it.next(); + boolean isPreview = PREVIEW_PATTERN.matcher( version.toString() ).matches(); + + // encountered a version in same Major.Minor version, remove it. + if ( version.toString().startsWith( current + "." ) || version.toString().startsWith( current + "-" ) ) + { + if ( needOneMore && !isPreview ) + { + needOneMore = false; + continue; + } + it.remove(); + continue; + } + + // if last version is a pre-release, also search for the last release. + needOneMore = isPreview; + + // encountered a new Major.Minor version, keep it. + int indexOf = StringUtils.ordinalIndexOf( version.toString(), ".", Segment.MINOR.value() + 1 ); + if ( indexOf > -1 ) + { + current = version.toString().substring( 0, indexOf ); + continue; + } + indexOf = version.toString().indexOf( "-" ); + if ( indexOf > -1 ) + { + current = version.toString().substring( 0, indexOf ); + } + } + } + return result.toArray( new ArtifactVersion[0] ); } diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java index 7fe827e9fd..36ceea3963 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/AbstractVersionsReportRenderer.java @@ -64,6 +64,20 @@ public abstract class AbstractVersionsReportRenderer extends VersionsReportRe protected ArtifactVersionsCache allUpdatesCache = new ArtifactVersionsCache( AbstractVersionDetails::getAllUpdates ); + protected boolean verboseSummary = true; + + protected boolean verboseDetail = true; + + public void setVerboseSummary( boolean verboseSummary ) + { + this.verboseSummary = verboseSummary; + } + + public void setVerboseDetail( boolean verboseDetail ) + { + this.verboseDetail = verboseDetail; + } + /** * Constructor to be called by the dependency injection framework * @param i18n i18n object to be injected @@ -136,59 +150,11 @@ protected void renderOverview() */ protected void renderOverviewTableRow( Q stats ) { - sink.tableCell(); - renderSuccessIcon(); - sink.tableCell_(); - sink.tableCell(); - sink.text( getText( "report.overview.numUpToDate" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( stats.getUpToDate() ) ); - sink.tableCell_(); - sink.tableRow_(); - sink.tableRow(); - sink.tableCell(); - renderWarningIcon(); - sink.tableCell_(); - sink.tableCell(); - sink.text( getText( "report.overview.numNewerVersionAvailable" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( stats.getAny() ) ); - sink.tableCell_(); - sink.tableRow_(); - sink.tableRow(); - sink.tableCell(); - renderWarningIcon(); - sink.tableCell_(); - sink.tableCell(); - sink.text( getText( "report.overview.numNewerIncrementalAvailable" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( stats.getIncremental() ) ); - sink.tableCell_(); - sink.tableRow_(); - sink.tableRow(); - sink.tableCell(); - renderWarningIcon(); - sink.tableCell_(); - sink.tableCell(); - sink.text( getText( "report.overview.numNewerMinorAvailable" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( stats.getMinor() ) ); - sink.tableCell_(); - sink.tableRow_(); - sink.tableRow(); - sink.tableCell(); - renderWarningIcon(); - sink.tableCell_(); - sink.tableCell(); - sink.text( getText( "report.overview.numNewerMajorAvailable" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( stats.getMajor() ) ); - sink.tableCell_(); + renderStatRow( "report.overview.numUpToDate", stats.getUpToDate(), true ); + renderStatRow( "report.overview.numNewerVersionAvailable", stats.getAny(), false ); + renderStatRow( "report.overview.numNewerIncrementalAvailable", stats.getIncremental(), false ); + renderStatRow( "report.overview.numNewerMinorAvailable", stats.getMinor(), false ); + renderStatRow( "report.overview.numNewerMajorAvailable", stats.getMajor(), false ); } /** @@ -242,6 +208,10 @@ protected void renderSummaryTableRow( Dependency artifact, ArtifactVersions deta { ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() ); boolean upToDate = allUpdates == null || allUpdates.length == 0; + if ( upToDate && !verboseSummary ) + { + return; + } sink.tableRow(); sink.tableCell(); diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java index 2d8948280a..68db7c40c6 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java @@ -23,12 +23,15 @@ import java.util.Map; import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.doxia.sink.Sink; import org.apache.maven.model.Dependency; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; import org.codehaus.plexus.i18n.I18N; +import static java.util.Optional.empty; + /** * @param type of the model * @since 1.0-beta-1 @@ -91,6 +94,13 @@ protected OverviewStats computeOverviewStats() protected void renderDependencyDetail( Dependency artifact, ArtifactVersions details ) { + ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() ); + boolean upToDate = allUpdates == null || allUpdates.length == 0; + if ( upToDate && !verboseDetail ) + { + return; + } + sink.section2(); sink.sectionTitle2(); sink.text( ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ) ); @@ -98,4 +108,5 @@ protected void renderDependencyDetail( Dependency artifact, ArtifactVersions det renderDependencyDetailTable( artifact, details, true ); sink.section2_(); } + } diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java index 7f38c91d5b..47ba882dc8 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java @@ -143,17 +143,17 @@ protected void renderSummaryTableHeader( boolean hasScope, boolean hasType ) protected void renderOverviewTableRow( T stats ) { super.renderOverviewTableRow( stats ); - sink.tableCell(); - sink.text( getText( "report.overview.numNewerDependenciesAvailable" ) ); - sink.tableCell_(); - sink.tableCell(); - sink.text( Integer.toString( ( (PluginOverviewStats) stats ).getDependencies() ) ); - sink.tableCell_(); + super.renderStatRow( "report.overview.numNewerDependenciesAvailable", + ( (PluginOverviewStats) stats ).getDependencies(), false ); } protected void renderSummaryTableRow( Dependency artifact, PluginUpdatesDetails details ) { boolean upToDate = !details.isUpdateAvailable(); + if ( upToDate && !verboseSummary ) + { + return; + } sink.tableRow(); sink.tableCell(); @@ -237,6 +237,12 @@ protected void renderSummaryTableRow( Dependency artifact, PluginUpdatesDetails @SuppressWarnings( "checkstyle:MethodLength" ) private void renderPluginDetail( Dependency artifact, PluginUpdatesDetails details ) { + boolean upToDate = !details.isUpdateAvailable(); + if ( upToDate && !verboseDetail ) + { + return; + } + sink.section2(); sink.sectionTitle2(); sink.text( MessageFormat.format( getText( "report.plugin" ), diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java index ddb7287e82..e84dbca37c 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java @@ -118,6 +118,10 @@ private void renderPropertySummaryTableRow( Property property, PropertyVersions { ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() ); boolean upToDate = allUpdates == null || allUpdates.length == 0; + if ( upToDate && !verboseSummary ) + { + return; + } sink.tableRow(); sink.tableCell(); @@ -425,6 +429,13 @@ else if ( newestUpdateCache.get( details, of( MAJOR ) ) != null ) private void renderPropertyDetail( Property property, PropertyVersions details ) { + ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() ); + boolean upToDate = allUpdates == null || allUpdates.length == 0; + if ( upToDate && !verboseDetail ) + { + return; + } + sink.section2(); sink.sectionTitle2(); sink.text( "${" + property.getName() + "}" ); diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java b/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java index c762b2efb8..ca20d26a10 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactory.java @@ -37,9 +37,12 @@ public interface ReportRendererFactory * @param sink sink to use for rendering * @param locale locale to use for rendering * @param model data to render + * @param verboseSummary false if up to date rows should be filtered out of report table. + * @param verboseDetail false if up to date rows should be filtered out of report table. * @return new report renderer * @throws IllegalArgumentException thrown if the report with the given name could not be found */ - T createReportRenderer( String reportName, Sink sink, Locale locale, U model ) + T createReportRenderer( String reportName, Sink sink, Locale locale, U model, + boolean verboseSummary, boolean verboseDetail ) throws IllegalArgumentException; } diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java b/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java index d1344a811c..4e25164405 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/ReportRendererFactoryImpl.java @@ -57,29 +57,37 @@ public ReportRendererFactoryImpl( I18N i18N ) */ @Override @SuppressWarnings( "unchecked" ) - public T createReportRenderer( String reportName, Sink sink, Locale locale, U model ) + public T createReportRenderer( String reportName, Sink sink, Locale locale, U model, + boolean verboseSummary, boolean verboseDetail ) throws IllegalArgumentException { + AbstractVersionsReportRenderer renderer = null; if ( DEPENDENCY_UPDATES_REPORT.equals( reportName ) ) { - return (T) new DependencyUpdatesReportRenderer<>( i18N, sink, locale, reportName, + renderer = new DependencyUpdatesReportRenderer<>( i18N, sink, locale, reportName, (DependencyUpdatesModel) model ); } if ( PLUGIN_UPDATES_REPORT.equals( reportName ) ) { - return (T) new PluginUpdatesReportRenderer( i18N, sink, locale, reportName, + renderer = new PluginUpdatesReportRenderer( i18N, sink, locale, reportName, (PluginUpdatesModel) model ); } if ( PROPERTY_UPDATES_REPORT.equals( reportName ) ) { - return (T) new PropertyUpdatesReportRenderer( i18N, sink, locale, reportName, + renderer = new PropertyUpdatesReportRenderer( i18N, sink, locale, reportName, (PropertyUpdatesModel) model ); } if ( PARENT_UPDATES_REPORT.equals( reportName ) ) { - return (T) new ParentUpdatesReportRenderer( i18N, sink, locale, reportName, + renderer = new ParentUpdatesReportRenderer( i18N, sink, locale, reportName, (ParentUpdatesModel) model ); } + if ( renderer != null ) + { + renderer.setVerboseSummary( verboseSummary ); + renderer.setVerboseDetail( verboseDetail ); + return (T) renderer; + } throw new IllegalArgumentException( "Invalid report name: " + reportName ); } } diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java b/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java index 589cfa8a80..472a42c4d8 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/VersionsReportRendererBase.java @@ -86,6 +86,28 @@ protected void renderSuccessIcon() sink.figureGraphics( "images/icon_success_sml.gif" ); } + protected void renderStatRow( String textKey, int statCount, boolean forceSuccessIcon ) + { + sink.tableRow(); + sink.tableCell(); + if ( statCount == 0 || forceSuccessIcon ) + { + renderSuccessIcon(); + } + else + { + renderWarningIcon(); + } + sink.tableCell_(); + sink.tableCell(); + sink.text( getText( textKey ) ); + sink.tableCell_(); + sink.tableCell(); + sink.text( Integer.toString( statCount ) ); + sink.tableCell_(); + sink.tableRow_(); + } + protected boolean equals( ArtifactVersion v1, ArtifactVersion v2 ) { return v1 == v2 || ( v1 != null && v1.equals( v2 ) )