From 42617d08423bedce0329162207172a07595e64ef Mon Sep 17 00:00:00 2001 From: Bertrand Martin Date: Wed, 26 Jun 2019 20:28:26 +0200 Subject: [PATCH] [MSITE-842] $currentFileName and $alignedFileName are incorrect for Maven Report plugins that use several Sink instances * Ensured that the actual filename of the Sink being rendered is exposed in RenderingContext for multi-page reports * Subpages (or subsinks) in subdirectories are now supported This closes #8 --- src/it/projects/MSITE-842/invoker.properties | 21 ++++ src/it/projects/MSITE-842/pom.xml | 93 ++++++++++++++++ .../projects/MSITE-842/src/it/MSITE-842.html | 26 +++++ .../MSITE-842/src/it/another-page.html | 26 +++++ src/it/projects/MSITE-842/src/it/sub/sub.html | 26 +++++ .../org/apache/maven/plugins/it/MyReport.java | 100 ++++++++++++++++++ .../MSITE-842/src/site/show-properties.vm | 26 +++++ src/it/projects/MSITE-842/verify.groovy | 44 ++++++++ .../site/render/ReportDocumentRenderer.java | 68 ++++++++++-- 9 files changed, 420 insertions(+), 10 deletions(-) create mode 100644 src/it/projects/MSITE-842/invoker.properties create mode 100644 src/it/projects/MSITE-842/pom.xml create mode 100644 src/it/projects/MSITE-842/src/it/MSITE-842.html create mode 100644 src/it/projects/MSITE-842/src/it/another-page.html create mode 100644 src/it/projects/MSITE-842/src/it/sub/sub.html create mode 100644 src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java create mode 100644 src/it/projects/MSITE-842/src/site/show-properties.vm create mode 100644 src/it/projects/MSITE-842/verify.groovy diff --git a/src/it/projects/MSITE-842/invoker.properties b/src/it/projects/MSITE-842/invoker.properties new file mode 100644 index 00000000..ed62a613 --- /dev/null +++ b/src/it/projects/MSITE-842/invoker.properties @@ -0,0 +1,21 @@ +# 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. + +invoker.goals.1 = clean install +invoker.debug.1 = false +invoker.goals.2 = site +invoker.maven.version = 3.0+ diff --git a/src/it/projects/MSITE-842/pom.xml b/src/it/projects/MSITE-842/pom.xml new file mode 100644 index 00000000..470ef867 --- /dev/null +++ b/src/it/projects/MSITE-842/pom.xml @@ -0,0 +1,93 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.site.its + MSITE-842 + 1.0-SNAPSHOT + maven-plugin + + it-plugin-test Maven Mojo + http://maven.apache.org + + + UTF-8 + + + + + org.apache.maven.reporting + maven-reporting-impl + 3.0.0 + + + + + + + + + org.apache.maven.plugins + maven-site-plugin + @project.version@ + + + + + + + org.apache.maven.plugins + maven-site-plugin + + ${basedir}/src/site/show-properties.vm + + + + + + + + + + org.apache.maven.plugins.site.its + MSITE-842 + ${project.version} + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.9 + + + + true + + + + + + + + diff --git a/src/it/projects/MSITE-842/src/it/MSITE-842.html b/src/it/projects/MSITE-842/src/it/MSITE-842.html new file mode 100644 index 00000000..4aea73d8 --- /dev/null +++ b/src/it/projects/MSITE-842/src/it/MSITE-842.html @@ -0,0 +1,26 @@ + + +alignedFileName = MSITE-842.html + +currentFileName = MSITE-842.html + +getRelativePath() = . + +content = Main Sink diff --git a/src/it/projects/MSITE-842/src/it/another-page.html b/src/it/projects/MSITE-842/src/it/another-page.html new file mode 100644 index 00000000..b95cde39 --- /dev/null +++ b/src/it/projects/MSITE-842/src/it/another-page.html @@ -0,0 +1,26 @@ + + +alignedFileName = another-page.html + +currentFileName = another-page.html + +getRelativePath() = . + +content = Another Sink diff --git a/src/it/projects/MSITE-842/src/it/sub/sub.html b/src/it/projects/MSITE-842/src/it/sub/sub.html new file mode 100644 index 00000000..94838ae0 --- /dev/null +++ b/src/it/projects/MSITE-842/src/it/sub/sub.html @@ -0,0 +1,26 @@ + + +alignedFileName = ../sub/sub.html + +currentFileName = sub/sub.html + +getRelativePath() = .. + +content = Subdirectory Sink diff --git a/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java b/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java new file mode 100644 index 00000000..c00c6fc9 --- /dev/null +++ b/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java @@ -0,0 +1,100 @@ +package org.apache.maven.plugins.it; + +/* + * 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 org.apache.maven.doxia.sink.Sink; +import org.apache.maven.reporting.AbstractMavenReport; +import org.apache.maven.reporting.MavenReportException; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + +/** + * Goal which creates several pages in a report. + * + * @goal test + * @phase site + */ +public class MyReport + extends AbstractMavenReport +{ + + public String getOutputName() + { + return "MSITE-842"; + } + + public String getName( Locale locale ) + { + return "MSITE-842"; + } + + public String getDescription( Locale locale ) + { + return "Test Report for MSITE-842"; + } + + @Override + protected void executeReport( Locale locale ) throws MavenReportException + { + // Main Sink + Sink mainSink = getSink(); + if ( mainSink == null ) + { + throw new MavenReportException("Could not get the main Sink"); + } + + // Write to the main Sink + mainSink.text( "Main Sink" ); + + // Create a new sink! + Sink anotherSink; + try + { + anotherSink = getSinkFactory().createSink( outputDirectory, "another-page.html" ); + } + catch ( IOException e ) + { + throw new MavenReportException( "Could not create sink for another-page.html in " + + outputDirectory.getAbsolutePath(), e ); + } + + // Write to the other Sink + anotherSink.text( "Another Sink" ); + + // Create a new sink, in a subdirectory + File subDirectory = new File( outputDirectory, "sub" ); + Sink subDirectorySink; + try + { + subDirectorySink = getSinkFactory().createSink( subDirectory, "sub.html" ); + } + catch ( IOException e ) + { + throw new MavenReportException( "Could not create sink for sub.html in " + + subDirectory.getAbsolutePath(), e ); + } + + // Write to the sink in the subdirectory + subDirectorySink.text( "Subdirectory Sink" ); + + } +} diff --git a/src/it/projects/MSITE-842/src/site/show-properties.vm b/src/it/projects/MSITE-842/src/site/show-properties.vm new file mode 100644 index 00000000..f51d49eb --- /dev/null +++ b/src/it/projects/MSITE-842/src/site/show-properties.vm @@ -0,0 +1,26 @@ + + +alignedFileName = $alignedFileName + +currentFileName = $currentFileName + +getRelativePath() = $docRenderingContext.getRelativePath() + +content = $bodyContent diff --git a/src/it/projects/MSITE-842/verify.groovy b/src/it/projects/MSITE-842/verify.groovy new file mode 100644 index 00000000..508edb64 --- /dev/null +++ b/src/it/projects/MSITE-842/verify.groovy @@ -0,0 +1,44 @@ + +/* + * 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. + */ + +File resultFile; +File expectedFile; + +// Check MSITE-842.html (must exist and be exactly like the model +resultFile = new File(basedir, "target/site/MSITE-842.html"); +expectedFile = new File(basedir, "src/it/MSITE-842.html"); + +assert resultFile.exists() && resultFile.isFile() +assert resultFile.text.normalize().equals(expectedFile.text.normalize()) + +// Check another-page.html (must exist and be exactly like the model +resultFile = new File(basedir, "target/site/another-page.html"); +expectedFile = new File(basedir, "src/it/another-page.html"); + +assert resultFile.exists() && resultFile.isFile() +assert resultFile.text.normalize().equals(expectedFile.text.normalize()) + +// Check sub/sub.html (must exist and be exactly like the model +resultFile = new File(basedir, "target/site/sub/sub.html"); +expectedFile = new File(basedir, "src/it/sub/sub.html"); + +assert resultFile.exists() && resultFile.isFile() +assert resultFile.text.normalize().equals(expectedFile.text.normalize()) + diff --git a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java index a5e8201a..dcf6e078 100644 --- a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java +++ b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java @@ -46,6 +46,7 @@ import org.apache.maven.reporting.MavenReport; import org.apache.maven.reporting.MavenReportException; import org.apache.maven.reporting.exec.MavenReportExecution; +import org.codehaus.plexus.util.PathTool; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; @@ -65,7 +66,7 @@ public class ReportDocumentRenderer private final String reportMojoInfo; private final ClassLoader classLoader; - + private final Log log; public ReportDocumentRenderer( MavenReportExecution mavenReportExecution, RenderingContext renderingContext, @@ -95,7 +96,7 @@ public ReportDocumentRenderer( MavenReportExecution mavenReportExecution, Render /** * Get plugin information from report's Manifest. - * + * * @param report the Maven report * @return plugin information as Specification Title followed by Specification Version if set in Manifest and * supported by JVM @@ -108,7 +109,7 @@ private String getPluginInfo( MavenReport report ) { String title = pkg.getSpecificationTitle(); String version = pkg.getSpecificationVersion(); - + if ( title == null ) { return version; @@ -133,9 +134,9 @@ private static class MultiPageSubSink private String outputName; - MultiPageSubSink( File outputDir, String outputName, RenderingContext ctx ) + MultiPageSubSink( File outputDir, String outputName, RenderingContext context ) { - super( ctx ); + super( context ); this.outputName = outputName; this.outputDir = outputDir; } @@ -155,22 +156,56 @@ public File getOutputDir() private static class MultiPageSinkFactory implements SinkFactory { + /** + * The report that is (maybe) generating multiple pages + */ + private MavenReport report; + + /** + * The main RenderingContext, which is the base for the RenderingContext of subpages + */ private RenderingContext context; + /** + * List of sinks (subpages) associated to this report + */ private List sinks = new ArrayList(); - MultiPageSinkFactory( RenderingContext ctx ) + MultiPageSinkFactory( MavenReport report, RenderingContext context ) { - this.context = ctx; + this.report = report; + this.context = context; } + @Override public Sink createSink( File outputDir, String outputName ) { - MultiPageSubSink sink = new MultiPageSubSink( outputDir, outputName, context ); + // Create a new context, similar to the main one, but with a different output name + String outputRelativeToTargetSite = PathTool.getRelativeFilePath( + report.getReportOutputDirectory().getPath(), + new File( outputDir, outputName ).getPath() + ); + + RenderingContext subSinkContext = new RenderingContext( + context.getBasedir(), + context.getBasedirRelativePath(), + outputRelativeToTargetSite, + context.getParserId(), + context.getExtension(), + context.isEditable(), + context.getGenerator() + ); + + // Create a sink for this subpage, based on this new context + MultiPageSubSink sink = new MultiPageSubSink( outputDir, outputName, subSinkContext ); + + // Add it to the list of sinks associated to this report sinks.add( sink ); + return sink; } + @Override public Sink createSink( File arg0, String arg1, String arg2 ) throws IOException { @@ -178,6 +213,7 @@ public Sink createSink( File arg0, String arg1, String arg2 ) return null; } + @Override public Sink createSink( OutputStream arg0 ) throws IOException { @@ -185,6 +221,7 @@ public Sink createSink( OutputStream arg0 ) return null; } + @Override public Sink createSink( OutputStream arg0, String arg1 ) throws IOException { @@ -198,6 +235,7 @@ public List sinks() } } + @Override public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext ) throws RendererException, FileNotFoundException { @@ -213,7 +251,7 @@ public void renderDocument( Writer writer, Renderer renderer, SiteRenderingConte // main sink SiteRendererSink mainSink = new SiteRendererSink( renderingContext ); // sink factory, for multi-page reports that need sub-sinks - MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory( renderingContext ); + MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory( report, renderingContext ); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try @@ -288,6 +326,12 @@ else if ( generateMultiPage( locale, multiPageSinkFactory, mainSink ) ) outputName = mySink.getOutputName(); log.debug( " Rendering " + outputName ); + // Create directories if necessary + if ( !mySink.getOutputDir().exists() ) + { + mySink.getOutputDir().mkdirs(); + } + File outputFile = new File( mySink.getOutputDir(), outputName ); try ( Writer out = WriterFactory.newWriter( outputFile, siteRenderingContext.getOutputEncoding() ) ) @@ -313,7 +357,7 @@ else if ( generateMultiPage( locale, multiPageSinkFactory, mainSink ) ) /** * Try to generate report with extended multi-page API. - * + * * @return true if the report was compatible with the extended API */ private boolean generateMultiPage( Locale locale, SinkFactory sf, Sink sink ) @@ -344,16 +388,19 @@ private boolean generateMultiPage( Locale locale, SinkFactory sf, Sink sink ) } } + @Override public String getOutputName() { return renderingContext.getOutputName(); } + @Override public RenderingContext getRenderingContext() { return renderingContext; } + @Override public boolean isOverwrite() { // TODO: would be nice to query the report to see if it is modified @@ -363,6 +410,7 @@ public boolean isOverwrite() /** * @return true if the current report is external, false otherwise */ + @Override public boolean isExternalReport() { return report.isExternalReport();