From e39a53b06d3afa88c670b5cc1ce5d1d2d066c898 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 28 Oct 2022 15:34:34 +0200 Subject: [PATCH] Resolves #359: XML Property Updates Report --- pom.xml | 1 + .../invoker.properties | 2 + .../pom.xml | 63 +++++++ .../verify.groovy | 42 +++++ .../versions/DependencyUpdatesReportMojo.java | 3 +- .../versions/PropertyUpdatesReportMojo.java | 40 +++- .../versions/reporting/OverviewStats.java | 7 +- .../reporting/PluginOverviewStats.java | 5 +- .../PropertyUpdatesReportRenderer.java | 26 +-- .../xml/CommonXmlReportRendererUtils.java | 59 ++++++ .../DependencyUpdatesXmlReportRenderer.java | 23 +-- .../xml/PluginUpdatesXmlReportRenderer.java | 24 +-- .../xml/PropertyUpdatesXmlReportRenderer.java | 134 ++++++++++++++ src/main/mdo/dependency-updates-report.mdo | 1 - src/main/mdo/plugin-updates-report.mdo | 1 - src/main/mdo/property-updates-report.mdo | 171 ++++++++++++++++++ src/site/markdown/xsd/index.md | 4 + .../PropertyUpdatesReportMojoTest.java | 2 +- 18 files changed, 530 insertions(+), 78 deletions(-) create mode 100644 src/it/it-xml-property-updates-report-001/invoker.properties create mode 100644 src/it/it-xml-property-updates-report-001/pom.xml create mode 100644 src/it/it-xml-property-updates-report-001/verify.groovy create mode 100644 src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java create mode 100644 src/main/java/org/codehaus/mojo/versions/xml/PropertyUpdatesXmlReportRenderer.java create mode 100644 src/main/mdo/property-updates-report.mdo diff --git a/pom.xml b/pom.xml index 4fc1fcde21..e0582e6b44 100644 --- a/pom.xml +++ b/pom.xml @@ -349,6 +349,7 @@ src/main/mdo/rule.mdo src/main/mdo/dependency-updates-report.mdo src/main/mdo/plugin-updates-report.mdo + src/main/mdo/property-updates-report.mdo 2.1.0 diff --git a/src/it/it-xml-property-updates-report-001/invoker.properties b/src/it/it-xml-property-updates-report-001/invoker.properties new file mode 100644 index 0000000000..20deacedb7 --- /dev/null +++ b/src/it/it-xml-property-updates-report-001/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:property-updates-report +invoker.mavenOpts = -DpropertyUpdatesReportFormats=xml \ No newline at end of file diff --git a/src/it/it-xml-property-updates-report-001/pom.xml b/src/it/it-xml-property-updates-report-001/pom.xml new file mode 100644 index 0000000000..b5028b0422 --- /dev/null +++ b/src/it/it-xml-property-updates-report-001/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + localhost + it-xml-property-updates-report-001 + 1.0 + + + 1.0 + 2.0 + 3.0 + + + + + localhost + dummy-api + [${api-version},2.1-!) + + + localhost + dummy-impl + ${impl-version} + + + + + + + localhost + dummy-maven-plugin + ${plugin-version} + + + localhost + dummy-api + ${plugin-version} + + + + + + + diff --git a/src/it/it-xml-property-updates-report-001/verify.groovy b/src/it/it-xml-property-updates-report-001/verify.groovy new file mode 100644 index 0000000000..be2ccf00aa --- /dev/null +++ b/src/it/it-xml-property-updates-report-001/verify.groovy @@ -0,0 +1,42 @@ +class Fragment +{ + private fragment + + Fragment( output, beginString, endString ) + { + def startIndex = output.indexOf( beginString ) + def endIndex = output.indexOf( endString, startIndex ) + assert startIndex != -1 + assert endIndex != -1 + fragment = output.substring( startIndex, endIndex + endString.length() ) + } + + def contains( String searchString ) + { + return fragment.contains( searchString ) + } + + String toString() + { + return fragment + } +} + +def output = new File( basedir, 'target/property-updates-report.xml' ).text + .replaceAll( '\\n', '' ) + .replaceAll( '\\r', '' ) + +summary = new Fragment( output, '', '' ) + +assert summary.contains( '1' ) +assert summary.contains( '1' ) +assert summary.contains( '1' ) + +assert new Fragment( output, 'api-version', + '' ).contains( 'incremental available' ) + +assert new Fragment( output, 'impl-version', + '' ).contains( 'minor available' ) + +assert new Fragment( output, 'plugin-version', + '' ).contains( 'no new available' ) diff --git a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java index f7c1a30c99..69c5a66e67 100644 --- a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java @@ -224,10 +224,9 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() ! .collect( Collectors.joining( ", " ) ) ) ); } + DependencyUpdatesModel model = new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ); for ( String format : formats ) { - DependencyUpdatesModel model = - new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ); if ( "html".equals( format ) ) { rendererFactory.createReportRenderer( getOutputName(), sink, locale, model ).render(); diff --git a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java index 60f758678c..480949fefd 100644 --- a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java @@ -21,6 +21,10 @@ import javax.inject.Inject; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Locale; import java.util.Map; import java.util.TreeMap; @@ -40,6 +44,7 @@ import org.codehaus.mojo.versions.reporting.ReportRendererFactory; import org.codehaus.mojo.versions.reporting.model.PropertyUpdatesModel; import org.codehaus.mojo.versions.utils.PropertyComparator; +import org.codehaus.mojo.versions.xml.PropertyUpdatesXmlReportRenderer; import org.codehaus.plexus.i18n.I18N; /** @@ -93,6 +98,14 @@ public class PropertyUpdatesReportMojo extends AbstractVersionsReport subclass of {@linkplain ArtifactVersions} * @return instance of the {@linkplain OverviewStats} */ - public static T fromUpdates( Collection updates, - ArtifactVersionsCache cache ) + public static + T fromUpdates( Collection updates, ArtifactVersionsCache cache ) { OverviewStats stats = new OverviewStats(); updates.forEach( details -> @@ -93,7 +94,7 @@ else if ( getNewestUpdate( cache, details, of( MAJOR ) ) != null ) return (T) stats; } - protected static ArtifactVersion getNewestUpdate( ArtifactVersionsCache cache, + protected static ArtifactVersion getNewestUpdate( ArtifactVersionsCache cache, V details, Optional segment ) { diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java b/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java index 2d215b75cf..81dea82dd6 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java @@ -22,6 +22,7 @@ import java.util.Optional; import org.codehaus.mojo.versions.PluginUpdatesDetails; +import org.codehaus.mojo.versions.api.AbstractVersionDetails; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.ArtifactVersionsCache; import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; @@ -60,8 +61,8 @@ public void incrementDependencies() * @param always equal to {@linkplain org.codehaus.mojo.versions.PluginUpdatesDetails} * @return instance of the {@linkplain PluginOverviewStats}, initialised with the update information */ - public static T fromUpdates( Collection updates, - ArtifactVersionsCache cache ) + public static + T fromUpdates( Collection updates, ArtifactVersionsCache cache ) { PluginOverviewStats stats = new PluginOverviewStats(); updates.forEach( details -> 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..1ca1730906 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PropertyUpdatesReportRenderer.java @@ -396,31 +396,7 @@ protected void renderSummaryTableHeader( boolean hasScope, boolean hasType ) @Override protected OverviewStats computeOverviewStats() { - OverviewStats stats = new OverviewStats(); - model.getAllUpdates().values().forEach( details -> - { - if ( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null ) - { - stats.incrementAny(); - } - else if ( newestUpdateCache.get( details, of( INCREMENTAL ) ) != null ) - { - stats.incrementIncremental(); - } - else if ( newestUpdateCache.get( details, of( MINOR ) ) != null ) - { - stats.incrementMinor(); - } - else if ( newestUpdateCache.get( details, of( MAJOR ) ) != null ) - { - stats.incrementMajor(); - } - else - { - stats.incrementUpToDate(); - } - } ); - return stats; + return OverviewStats.fromUpdates( model.getAllUpdates().values(), newestUpdateCache ); } private void renderPropertyDetail( Property property, PropertyVersions details ) diff --git a/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java b/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java new file mode 100644 index 0000000000..e828d3d785 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/xml/CommonXmlReportRendererUtils.java @@ -0,0 +1,59 @@ +package org.codehaus.mojo.versions.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.api.AbstractVersionDetails; +import org.codehaus.mojo.versions.api.Segment; + +import static java.util.Optional.of; +import static java.util.Optional.ofNullable; + +/** + * Common utils for Xml report renderers + */ +class CommonXmlReportRendererUtils +{ + static void setSection( AbstractVersionDetails versions, Segment segment, Consumer> setterFunction ) + { + ofNullable( versions.getAllUpdates( of( segment ) ) ) + .map( v -> Arrays.stream( v ) + .map( ArtifactVersion::toString ) + .collect( Collectors.toList() ) ) + .ifPresent( setterFunction ); + } + + static String statusFor( String lastVersion, Collection incrementals, Collection minors ) + { + return lastVersion == null + ? "no new available" + : incrementals != null && !incrementals.isEmpty() + ? "incremental available" + : minors != null && !minors.isEmpty() + ? "minor available" + : "major available"; + } +} diff --git a/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java index dee099f597..d360a92ab1 100644 --- a/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java @@ -24,10 +24,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.function.Consumer; import java.util.stream.Collectors; import org.apache.maven.artifact.versioning.ArtifactVersion; @@ -36,7 +34,6 @@ import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.ArtifactVersionsCache; import org.codehaus.mojo.versions.api.ReportRenderer; -import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.reporting.OverviewStats; import org.codehaus.mojo.versions.reporting.model.DependencyInfo; import org.codehaus.mojo.versions.reporting.model.DependencyReportSummary; @@ -45,11 +42,12 @@ import org.codehaus.mojo.versions.reporting.model.io.xpp3.DependencyUpdatesReportXpp3Writer; import static java.util.Optional.empty; -import static java.util.Optional.of; import static java.util.Optional.ofNullable; import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; import static org.codehaus.mojo.versions.api.Segment.MAJOR; import static org.codehaus.mojo.versions.api.Segment.MINOR; +import static org.codehaus.mojo.versions.xml.CommonXmlReportRendererUtils.setSection; +import static org.codehaus.mojo.versions.xml.CommonXmlReportRendererUtils.statusFor; /** * XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available @@ -106,15 +104,6 @@ public void render() } } - private static void setSection( ArtifactVersions versions, Segment segment, Consumer> setterFunction ) - { - ofNullable( versions.getAllUpdates( of( segment ) ) ) - .map( v -> Arrays.stream( v ) - .map( ArtifactVersion::toString ) - .collect( Collectors.toList() ) ) - .ifPresent( setterFunction ); - } - private static List createDependencyInfo( Map versions ) { return versions.entrySet().stream().map( e -> @@ -134,13 +123,7 @@ private static List createDependencyInfo( Map> setterFunction ) - { - ofNullable( versions.getAllUpdates( of( segment ) ) ) - .map( v -> Arrays.stream( v ) - .map( ArtifactVersion::toString ) - .collect( Collectors.toList() ) ) - .ifPresent( setterFunction ); - } - private static List createPluginInfo( Map versions ) { return versions.entrySet().stream().map( e -> @@ -136,13 +124,7 @@ private static List createPluginInfo( Map createPropertyInfo( Map versions ) + { + return versions.entrySet().stream().map( e -> + new PropertyInfo() + {{ + setPropertyName( e.getKey().getName() ); + if ( e.getValue().getAssociations() != null + && e.getValue().getAssociations().length != 0 ) + { + setPropertyAssociations( Arrays.stream( e.getValue().getAssociations() ) + .map( a -> + { + PropertyAssociation pa = new PropertyAssociation(); + pa.setGroupId( a.getGroupId() ); + pa.setArtifactId( a.getArtifactId() ); + return pa; + } ).collect( Collectors.toList() ) ); + } + setCurrentVersion( e.getKey().getVersion() ); + ofNullable( e.getValue().getNewestUpdate( empty() ) ) + .map( ArtifactVersion::toString ).ifPresent( this::setLastVersion ); + + setSection( e.getValue(), INCREMENTAL, this::setIncrementals ); + setSection( e.getValue(), MINOR, this::setMinors ); + setSection( e.getValue(), MAJOR, this::setMajors ); + + setStatus( statusFor( getLastVersion(), getIncrementals(), getMinors() ) ); + }} ).collect( Collectors.toList() ); + } +} diff --git a/src/main/mdo/dependency-updates-report.mdo b/src/main/mdo/dependency-updates-report.mdo index c8f41b2d8d..ea30dcaceb 100644 --- a/src/main/mdo/dependency-updates-report.mdo +++ b/src/main/mdo/dependency-updates-report.mdo @@ -85,7 +85,6 @@ under the License. nextIncrementalAvailable - 1.0.0+ true String diff --git a/src/main/mdo/plugin-updates-report.mdo b/src/main/mdo/plugin-updates-report.mdo index 685d75e3d0..f311d2edcf 100644 --- a/src/main/mdo/plugin-updates-report.mdo +++ b/src/main/mdo/plugin-updates-report.mdo @@ -85,7 +85,6 @@ under the License. nextIncrementalAvailable - 1.0.0+ true String diff --git a/src/main/mdo/property-updates-report.mdo b/src/main/mdo/property-updates-report.mdo new file mode 100644 index 0000000000..9456e60280 --- /dev/null +++ b/src/main/mdo/property-updates-report.mdo @@ -0,0 +1,171 @@ + + + + + + property-updates-report + PropertyUpdatesReport + + XML version of the Property Updates Report + + + + + package + org.codehaus.mojo.versions.reporting.model + + + + + namespace + + + + + + PropertyUpdatesReport + + + summary + true + + PropertyReportSummary + 1 + + + + properties + + PropertyInfo + * + + + + + + + PropertyReportSummary + + + usingLastVersion + true + String + + + nextVersionAvailable + true + String + + + nextIncrementalAvailable + true + String + + + nextMinorAvailable + true + String + + + nextMajorAvailable + true + String + + + + + + PropertyInfo + + + propertyName + true + String + + + propertyAssociations + + PropertyAssociation + * + + + + currentVersion + String + + + lastVersion + String + + + any + + String + * + + + + incrementals + + String + * + + + + minors + + String + * + + + + majors + + String + * + + + + status + true + String + + + + + + PropertyAssociation + + + groupId + String + + + artifactId + String + + + + + + diff --git a/src/site/markdown/xsd/index.md b/src/site/markdown/xsd/index.md index ec05854c6a..db45b4459e 100644 --- a/src/site/markdown/xsd/index.md +++ b/src/site/markdown/xsd/index.md @@ -14,6 +14,10 @@ date: 2022-10-17 - [plugin-updates-report-2.1.0.xsd](plugin-updates-report-2.1.0.xsd) +## property-updates-report.xsd + +- [property-updates-report-2.1.0.xsd](property-updates-report-2.1.0.xsd) + ## rule.xsd - [rule-2.0.0.xsd](rule-2.0.0.xsd) diff --git a/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java b/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java index 4d498592d4..f0adb37f64 100644 --- a/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java @@ -74,7 +74,7 @@ public void testIncludeParentTrueShouldContainProperty() throws Exception } @Test - public void testIncludeParentFalseShouldNotCountainProperty() throws Exception + public void testIncludeParentFalseShouldNotContainProperty() throws Exception { OutputStream os = new ByteArrayOutputStream(); SinkFactory sinkFactory = new Xhtml5SinkFactory();