From ecf1b039a76a1b6eeda5e79e0885599e9e573cc8 Mon Sep 17 00:00:00 2001 From: Michael Osipov Date: Sat, 6 Aug 2022 23:46:02 +0200 Subject: [PATCH] [MSHARED-1099] Render with a skin when report is run in standalone mode --- pom.xml | 10 +++ .../maven/reporting/AbstractMavenReport.java | 90 +++++++++++++++---- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index f07f637..1d97cfc 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,11 @@ maven-core ${mavenVersion} + + org.apache.maven + maven-artifact + ${mavenVersion} + org.apache.maven maven-plugin-api @@ -116,6 +121,11 @@ doxia-core ${doxiaVersion} + + org.apache.maven.doxia + doxia-integration-tools + ${doxiaSitetoolsVersion} + org.apache.maven.doxia doxia-site-renderer diff --git a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java index cdb9a0b..e205d27 100644 --- a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java +++ b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java @@ -19,6 +19,8 @@ * under the License. */ +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.doxia.sink.Sink; import org.apache.maven.doxia.sink.SinkFactory; import org.apache.maven.doxia.site.decoration.DecorationModel; @@ -27,6 +29,8 @@ import org.apache.maven.doxia.siterenderer.RenderingContext; import org.apache.maven.doxia.siterenderer.SiteRenderingContext; import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink; +import org.apache.maven.doxia.tools.SiteTool; +import org.apache.maven.doxia.tools.SiteToolException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; @@ -35,12 +39,15 @@ import org.apache.maven.shared.utils.WriterFactory; import org.codehaus.plexus.util.ReaderFactory; +import static org.apache.maven.shared.utils.logging.MessageUtils.buffer; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; @@ -90,6 +97,24 @@ public abstract class AbstractMavenReport @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true ) private String outputEncoding; + /** + * The local repository. + */ + @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) + protected ArtifactRepository localRepository; + + /** + * Remote repositories used for the project. + */ + @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true ) + protected List remoteRepositories; + + /** + * SiteTool. + */ + @Component + protected SiteTool siteTool; + /** * Doxia Site Renderer component. */ @@ -126,19 +151,17 @@ public void execute() Locale locale = Locale.getDefault(); - SiteRenderingContext siteContext = new SiteRenderingContext(); - siteContext.setDecoration( new DecorationModel() ); - siteContext.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" ); - siteContext.setLocale( locale ); - siteContext.setTemplateProperties( getTemplateProperties() ); + try + { + SiteRenderingContext siteContext = createSiteRenderingContext( locale ); - // TODO Replace null with real value - RenderingContext context = new RenderingContext( outputDirectory, filename, null ); + // copy resources + getSiteRenderer().copyResources( siteContext, outputDirectory ); - SiteRendererSink sink = new SiteRendererSink( context ); + // TODO Replace null with real value + RenderingContext docRenderingContext = new RenderingContext( outputDirectory, filename, null ); - try - { + SiteRendererSink sink = new SiteRendererSink( docRenderingContext ); generate( sink, null, locale ); @@ -147,12 +170,16 @@ public void execute() outputDirectory.mkdirs(); try ( Writer writer = - new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ), - getOutputEncoding() ) ) + new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ), + getOutputEncoding() ) ) { + // render report getSiteRenderer().mergeDocumentIntoSite( writer, sink, siteContext ); } } + + // copy generated resources also + getSiteRenderer().copyResources( siteContext, outputDirectory ); } catch ( RendererException | IOException | MavenReportException e ) { @@ -161,14 +188,14 @@ public void execute() } } - /** - * create template properties like done in maven-site-plugin's - * AbstractSiteRenderingMojo.createSiteRenderingContext( Locale ) - * @return properties - */ - private Map getTemplateProperties() + private SiteRenderingContext createSiteRenderingContext( Locale locale ) + throws MavenReportException, IOException { + DecorationModel decorationModel = new DecorationModel(); + Map templateProperties = new HashMap<>(); + // We tell the skin that we are rendering in standalone mode + templateProperties.put( "standalone", Boolean.TRUE ); templateProperties.put( "project", getProject() ); templateProperties.put( "inputEncoding", getInputEncoding() ); templateProperties.put( "outputEncoding", getOutputEncoding() ); @@ -177,7 +204,32 @@ private Map getTemplateProperties() { templateProperties.put( (String) entry.getKey(), entry.getValue() ); } - return templateProperties; + + SiteRenderingContext context; + try + { + Artifact skinArtifact = + siteTool.getSkinArtifactFromRepository( localRepository, remoteRepositories, decorationModel ); + + getLog().info( buffer().a( "Rendering content with " ).strong( skinArtifact.getId() + + " skin" ).a( '.' ).toString() ); + + context = siteRenderer.createContextForSkin( skinArtifact, templateProperties, decorationModel, + project.getName(), locale ); + } + catch ( SiteToolException e ) + { + throw new MavenReportException( "Failed to retrieve skin artifact", e ); + } + catch ( RendererException e ) + { + throw new MavenReportException( "Failed to create context for skin", e ); + } + + // Generate static site + context.setRootDirectory( project.getBasedir() ); + + return context; } /**