Skip to content

Commit

Permalink
Resolves mojohaus#359: XML Property Updates Report
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Oct 29, 2022
1 parent 6647b8d commit 01aa121
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 78 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
<model>src/main/mdo/rule.mdo</model>
<model>src/main/mdo/dependency-updates-report.mdo</model>
<model>src/main/mdo/plugin-updates-report.mdo</model>
<model>src/main/mdo/property-updates-report.mdo</model>
</models>
<version>2.1.0</version>
</configuration>
Expand Down
2 changes: 2 additions & 0 deletions src/it/it-xml-property-updates-report-001/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:property-updates-report
invoker.mavenOpts = -DpropertyUpdatesReportFormats=xml
63 changes: 63 additions & 0 deletions src/it/it-xml-property-updates-report-001/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>it-xml-property-updates-report-001</artifactId>
<version>1.0</version>

<properties>
<api-version>1.0</api-version>
<impl-version>2.0</impl-version>
<plugin-version>3.0</plugin-version>
</properties>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>[${api-version},2.1-!)</version>
</dependency>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-impl</artifactId>
<version>${impl-version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>${plugin-version}</version>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${plugin-version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
42 changes: 42 additions & 0 deletions src/it/it-xml-property-updates-report-001/verify.groovy
Original file line number Diff line number Diff line change
@@ -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, '<summary>', '</summary>' )

assert summary.contains( '<usingLastVersion>1</usingLastVersion>' )
assert summary.contains( '<nextIncrementalAvailable>1</nextIncrementalAvailable>' )
assert summary.contains( '<nextMinorAvailable>1</nextMinorAvailable>' )

assert new Fragment( output, '<propertyName>api-version</propertyName>',
'</property>' ).contains( '<status>incremental available</status>' )

assert new Fragment( output, '<propertyName>impl-version</propertyName>',
'</property>' ).contains( '<status>minor available</status>' )

assert new Fragment( output, '<propertyName>plugin-version</propertyName>',
'</property>' ).contains( '<status>no new available</status>' )
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -93,6 +98,14 @@ public class PropertyUpdatesReportMojo extends AbstractVersionsReport<PropertyUp
@Parameter( property = "includeParent", defaultValue = "true" )
private boolean includeParent = true;

/**
* Report formats (html and/or xml). HTML by default.
*
* @since 2.14.0
*/
@Parameter( property = "propertyUpdatesReportFormats", defaultValue = "html" )
protected String[] formats = new String[] {"html"};

@Inject
protected PropertyUpdatesReportMojo( I18N i18n, RepositorySystem repositorySystem,
ArtifactResolver artifactResolver,
Expand Down Expand Up @@ -142,8 +155,31 @@ protected void doGenerateReport( Locale locale, Sink sink )
{
throw new MavenReportException( e.getMessage(), e );
}
rendererFactory.createReportRenderer( getOutputName(), getSink(), locale,
new PropertyUpdatesModel( updateSet ) ).render();
PropertyUpdatesModel model = new PropertyUpdatesModel( updateSet );
for ( String format : formats )
{
if ( "html".equals( format ) )
{
rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, model ).render();
}
else if ( "xml".equals( format ) )
{
Path outputDir = Paths.get( getProject().getBuild().getDirectory() );
if ( !Files.exists( outputDir ) )
{
try
{
Files.createDirectories( outputDir );
}
catch ( IOException e )
{
throw new MavenReportException( "Could not create the output directory" );
}
}
Path outputFile = outputDir.resolve( getOutputName() + ".xml" );
new PropertyUpdatesXmlReportRenderer( model, outputFile ).render();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Optional;

import org.apache.maven.artifact.versioning.ArtifactVersion;
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.api.Segment;
Expand Down Expand Up @@ -63,8 +64,8 @@ public class OverviewStats
* @param <V> subclass of {@linkplain ArtifactVersions}
* @return instance of the {@linkplain OverviewStats}
*/
public static <T extends OverviewStats, V extends ArtifactVersions> T fromUpdates( Collection<V> updates,
ArtifactVersionsCache cache )
public static <T extends OverviewStats, V extends AbstractVersionDetails>
T fromUpdates( Collection<V> updates, ArtifactVersionsCache cache )
{
OverviewStats stats = new OverviewStats();
updates.forEach( details ->
Expand Down Expand Up @@ -93,7 +94,7 @@ else if ( getNewestUpdate( cache, details, of( MAJOR ) ) != null )
return (T) stats;
}

protected static <V extends ArtifactVersions> ArtifactVersion getNewestUpdate( ArtifactVersionsCache cache,
protected static <V extends AbstractVersionDetails> ArtifactVersion getNewestUpdate( ArtifactVersionsCache cache,
V details,
Optional<Segment> segment )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,8 +61,8 @@ public void incrementDependencies()
* @param <V> always equal to {@linkplain org.codehaus.mojo.versions.PluginUpdatesDetails}
* @return instance of the {@linkplain PluginOverviewStats}, initialised with the update information
*/
public static <T extends OverviewStats, V extends ArtifactVersions> T fromUpdates( Collection<V> updates,
ArtifactVersionsCache cache )
public static <T extends OverviewStats, V extends AbstractVersionDetails>
T fromUpdates( Collection<V> updates, ArtifactVersionsCache cache )
{
PluginOverviewStats stats = new PluginOverviewStats();
updates.forEach( details ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<String>> 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";
}
}
Loading

0 comments on commit 01aa121

Please sign in to comment.