Skip to content

Commit

Permalink
[MSHARED-1099] Render with a skin when report is run in standalone mode
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Aug 6, 2022
1 parent df5e068 commit ecf1b03
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down Expand Up @@ -116,6 +121,11 @@
<artifactId>doxia-core</artifactId>
<version>${doxiaVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-integration-tools</artifactId>
<version>${doxiaSitetoolsVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
Expand Down
90 changes: 71 additions & 19 deletions src/main/java/org/apache/maven/reporting/AbstractMavenReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<ArtifactRepository> remoteRepositories;

/**
* SiteTool.
*/
@Component
protected SiteTool siteTool;

/**
* Doxia Site Renderer component.
*/
Expand Down Expand Up @@ -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 );

Expand All @@ -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 )
{
Expand All @@ -161,14 +188,14 @@ public void execute()
}
}

/**
* create template properties like done in maven-site-plugin's
* <code>AbstractSiteRenderingMojo.createSiteRenderingContext( Locale )</code>
* @return properties
*/
private Map<String, Object> getTemplateProperties()
private SiteRenderingContext createSiteRenderingContext( Locale locale )
throws MavenReportException, IOException
{
DecorationModel decorationModel = new DecorationModel();

Map<String, Object> 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() );
Expand All @@ -177,7 +204,32 @@ private Map<String, Object> 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;
}

/**
Expand Down

0 comments on commit ecf1b03

Please sign in to comment.