Skip to content

Commit

Permalink
#754: Use Modello for XML reports
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Oct 16, 2022
1 parent 954a853 commit 5824363
Show file tree
Hide file tree
Showing 21 changed files with 1,115 additions and 555 deletions.
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
<artifactId>modello-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<id>generate-rule</id>
<phase>generate-sources</phase>
<goals>
<!-- Generate the xpp3 reader code -->
Expand All @@ -323,6 +323,29 @@
<!-- Generate the Java sources for the model itself -->
<goal>java</goal>
</goals>
<configuration>
<models>
<model>src/main/mdo/rule.mdo</model>
</models>
<version>2.0.0</version>
</configuration>
</execution>
<execution>
<id>generate-reporting</id>
<phase>generate-sources</phase>
<goals>
<!-- Generate the writer code -->
<goal>xpp3-writer</goal>
<!-- Generate the Java sources for the model itself -->
<goal>java</goal>
</goals>
<configuration>
<models>
<model>src/main/mdo/dependency-updates-report.mdo</model>
<model>src/main/mdo/plugin-updates-report.mdo</model>
</models>
<version>2.0.0</version>
</configuration>
</execution>
<execution>
<id>site-doc</id>
Expand All @@ -345,6 +368,8 @@
<configuration>
<models>
<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>
</models>
<version>2.0.0</version>
</configuration>
Expand Down Expand Up @@ -392,6 +417,8 @@
<excludePackageNames>
org.codehaus.mojo.versions.model,
org.codehaus.mojo.versions.model.io.xpp3
org.codehaus.mojo.versions.reporting.model,
org.codehaus.mojo.versions.reporting.model.io.xpp3,
</excludePackageNames>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:dependency-updates-report
invoker.mavenOpts = -DdependencyUpdatesReportFormats=xml
16 changes: 16 additions & 0 deletions src/it/it-dependency-updates-report-issue-755/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<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>dummy-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

</project>
5 changes: 5 additions & 0 deletions src/it/it-dependency-updates-report-issue-755/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
report = new File( basedir, "target/dependency-updates-report.xml" ).text.replaceAll( '\\s*', '' )

assert report.contains( '<incremental>1.0.1</incremental>' )
assert report.contains( '<minor>1.1</minor>' )
assert report.contains( '<major>2.0</major>' )
2 changes: 2 additions & 0 deletions src/it/it-plugin-updates-report-issue-755/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:plugin-updates-report
invoker.mavenOpts = -DpluginUpdatesReportFormats=xml
18 changes: 18 additions & 0 deletions src/it/it-plugin-updates-report-issue-755/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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>dummy-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</build>

</project>
5 changes: 5 additions & 0 deletions src/it/it-plugin-updates-report-issue-755/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
report = new File( basedir, "target/plugin-updates-report.xml" ).text.replaceAll( '\\s*', '' )

assert report.contains( '<major>3.0.0</major>' )
assert report.contains( '<minor>2.3</minor>' )
assert report.contains( '<incremental>2.2.2</incremental>' )
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import javax.inject.Inject;

import java.io.File;
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.Set;
Expand All @@ -39,10 +42,10 @@
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlRenderer;
import org.codehaus.mojo.versions.reporting.ReportRendererFactory;
import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer;
import org.codehaus.plexus.i18n.I18N;

import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -205,27 +208,28 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() !

for ( String format : formats )
{
DependencyUpdatesModel model =
new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates );
if ( "html".equals( format ) )
{
rendererFactory.createReportRenderer( getOutputName(), sink, locale,
new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ) )
.render();
rendererFactory.createReportRenderer( getOutputName(), sink, locale, model ).render();
}
else if ( "xml".equals( format ) )
{
File outputDir = new File( getProject().getBuild().getDirectory() );
if ( !outputDir.exists() )
Path outputDir = Paths.get( getProject().getBuild().getDirectory() );
if ( !Files.exists( outputDir ) )
{
if ( !outputDir.mkdirs() )
try
{
throw new MavenReportException( "Could not create output directory" );
Files.createDirectories( outputDir );
}
catch ( IOException e )
{
throw new MavenReportException( "Could not create the output directory" );
}
}
String outputFile = outputDir.getAbsolutePath() + File.separator + getOutputName() + ".xml";
DependencyUpdatesXmlRenderer xmlGenerator =
new DependencyUpdatesXmlRenderer( dependencyUpdates, dependencyManagementUpdates,
outputFile );
xmlGenerator.render();
Path outputFile = outputDir.resolve( getOutputName() + ".xml" );
new DependencyUpdatesXmlReportRenderer( model, outputFile ).render();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import javax.inject.Inject;

import java.io.File;
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.Set;
Expand All @@ -38,10 +41,10 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.xml.PluginUpdatesXmlRenderer;
import org.codehaus.mojo.versions.reporting.ReportRendererFactory;
import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel;
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.mojo.versions.xml.PluginUpdatesXmlReportRenderer;
import org.codehaus.plexus.i18n.I18N;

import static org.codehaus.mojo.versions.utils.MiscUtils.filter;
Expand Down Expand Up @@ -166,28 +169,29 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx
plugin -> plugin.getVersions().length > 1 );
}

PluginUpdatesModel model = new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates );
for ( String format : formats )
{
if ( "html".equals( format ) )
{
rendererFactory.createReportRenderer( getOutputName(), getSink(), locale,
new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates ) )
.render();
rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, model ).render();
}
else if ( "xml".equals( format ) )
{
File outputDir = new File( getProject().getBuild().getDirectory() );
if ( !outputDir.exists() )
Path outputDir = Paths.get( getProject().getBuild().getDirectory() );
if ( !Files.exists( outputDir ) )
{
if ( !outputDir.mkdirs() )
try
{
throw new MavenReportException( "Could not create output directory" );
Files.createDirectories( outputDir );
}
catch ( IOException e )
{
throw new MavenReportException( "Could not create the output directory" );
}
}
String outputFile = outputDir.getAbsolutePath() + File.separator + getOutputName() + ".xml";
PluginUpdatesXmlRenderer xmlGenerator =
new PluginUpdatesXmlRenderer( pluginUpdates, pluginManagementUpdates, outputFile );
xmlGenerator.render();
Path outputFile = outputDir.resolve( getOutputName() + ".xml" );
new PluginUpdatesXmlReportRenderer( model, outputFile ).render();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel;
import org.codehaus.plexus.i18n.I18N;

import static java.util.Optional.of;
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.api.Segment.SUBINCREMENTAL;

/**
* @param <K> type of the model
* @since 1.0-beta-1
Expand Down Expand Up @@ -92,31 +86,7 @@ protected void renderTable( String titleKey, Map<Dependency, ArtifactVersions> c
@Override
protected OverviewStats computeOverviewStats()
{
OverviewStats stats = new OverviewStats();
model.getAllUpdates().values().forEach( details ->
{
if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
else
{
stats.incrementUpToDate();
}
} );
return stats;
return OverviewStats.fromUpdates( model.getAllUpdates().values(), oldestUpdateCache );
}

protected void renderDependencyDetail( Dependency artifact, ArtifactVersions details )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@
* under the License.
*/

import java.util.Collection;
import java.util.Optional;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.ArtifactVersionsCache;
import org.codehaus.mojo.versions.api.Segment;

import static java.util.Optional.of;
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.api.Segment.SUBINCREMENTAL;

/**
* Represents summary stats
*
* @author Andrzej Jarmoniuk
*/
class OverviewStats
public class OverviewStats
{
private int major;

Expand All @@ -37,6 +51,55 @@ class OverviewStats

private int upToDate;

/**
* Creates a {@linkplain OverviewStats} instance based on the collection of version updates in
* the argument
*
* @param updates collection of all version updates, typically from
* {@linkplain org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel#getAllUpdates()}
* @param cache if not null, cache to retrieve the version information, initialised with
* the {@link ArtifactVersions#getOldestUpdate(Optional)} update information
* @param <T> subclass of {@linkplain 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 )
{
OverviewStats stats = new OverviewStats();
updates.forEach( details ->
{
if ( getOldestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( getOldestUpdate( cache, details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( getOldestUpdate( cache, details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
else
{
stats.incrementUpToDate();
}
} );
return (T) stats;
}

protected static <V extends ArtifactVersions> ArtifactVersion getOldestUpdate( ArtifactVersionsCache cache,
V details,
Optional<Segment> segment )
{
return cache != null ? cache.get( details, segment ) : details.getOldestUpdate( segment );
}

public int getMajor()
{
return major;
Expand Down
Loading

0 comments on commit 5824363

Please sign in to comment.